From 21c64d390d6b45e78dda81136748a1c853f3819e Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 18 Jul 2024 01:13:48 +0200 Subject: [PATCH] Fixed SIGSEGV when dropping Channel from memory due to memory corruption --- .../jetpackduba/gitnuro/credentials/SshProcess.kt | 10 ++++------ .../gitnuro/credentials/SshRemoteSession.kt | 10 +++++----- .../gitnuro/ssh/libssh/ChannelWrapper.kt | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshProcess.kt b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshProcess.kt index 537bd1e..24b0f7a 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshProcess.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshProcess.kt @@ -42,17 +42,15 @@ class SshProcess : Process() { check(!isRunning()) println("exitValue called") - channel.close() - return 0 } override fun destroy() { - if (channel.isOpen()) { - channel.close() - } + closeChannel() + } - println("Destroy called") + fun closeChannel() { + channel.close() } private fun isRunning(): Boolean { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt index a7ad9cf..c6faaf9 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt @@ -12,20 +12,20 @@ private const val NOT_EXPLICIT_PORT = -1 class SshRemoteSession @Inject constructor( private val credentialsStateManager: CredentialsStateManager, ) : RemoteSession { - private var session: Session? = null - + private lateinit var session: Session + private lateinit var process: SshProcess override fun exec(commandName: String, timeout: Int): Process { println("Running command $commandName") - val session = this.session ?: throw Exception("Session is null") - val process = SshProcess() + process = SshProcess() process.setup(session, commandName) return process } override fun disconnect() { - session?.disconnect() + process.closeChannel() + session.disconnect() } fun setup(uri: URIish) { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/ChannelWrapper.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/ChannelWrapper.kt index 4769df7..a94b8e5 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/ChannelWrapper.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/ChannelWrapper.kt @@ -5,10 +5,13 @@ import Session import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputErrStream import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelInputStream import com.jetpackduba.gitnuro.ssh.libssh.streams.SshChannelOutputStream +import java.util.concurrent.Semaphore class ChannelWrapper internal constructor(sshSession: Session) { private val channel = Channel.new(sshSession) + private var isClosed = false + private var closeMutex = Semaphore(1) val outputStream = SshChannelOutputStream(channel) val inputStream = SshChannelInputStream(channel) val errorOutputStream = SshChannelInputErrStream(channel) @@ -26,7 +29,15 @@ class ChannelWrapper internal constructor(sshSession: Session) { } fun close() { - channel.closeChannel() - channel.close() + closeMutex.acquire() + try { + if (!isClosed) { + channel.closeChannel() + channel.close() + isClosed = true + } + } finally { + closeMutex.release() + } } } \ No newline at end of file