1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 17:14:27 +00:00

Fix one user may block entire listen loop for builtin SSH

This commit is contained in:
Unknwon 2016-02-01 12:10:49 -05:00
parent 5d192c2ebf
commit 3abad75a1b
3 changed files with 37 additions and 24 deletions

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.8.26.0131" const APP_VER = "0.8.26.0201"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -51,7 +51,7 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
case "env": case "env":
args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v") args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v")
if len(args) != 2 { if len(args) != 2 {
log.Warn("Invalid env arguments: '%#v'", args) log.Warn("SSH: Invalid env arguments: '%#v'", args)
continue continue
} }
args[0] = strings.TrimLeft(args[0], "\x04") args[0] = strings.TrimLeft(args[0], "\x04")
@ -63,31 +63,31 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
case "exec": case "exec":
cmdName := strings.TrimLeft(payload, "'()") cmdName := strings.TrimLeft(payload, "'()")
os.Setenv("SSH_ORIGINAL_COMMAND", cmdName) os.Setenv("SSH_ORIGINAL_COMMAND", cmdName)
log.Trace("Payload: %v", cmdName) log.Trace("SSH: Payload: %v", cmdName)
args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf} args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf}
log.Trace("Arguments: %v", args) log.Trace("SSH: Arguments: %v", args)
cmd := exec.Command(setting.AppPath, args...) cmd := exec.Command(setting.AppPath, args...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Error(3, "StdoutPipe: %v", err) log.Error(3, "SSH: StdoutPipe: %v", err)
return return
} }
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
log.Error(3, "StderrPipe: %v", err) log.Error(3, "SSH: StderrPipe: %v", err)
return return
} }
input, err := cmd.StdinPipe() input, err := cmd.StdinPipe()
if err != nil { if err != nil {
log.Error(3, "StdinPipe: %v", err) log.Error(3, "SSH: StdinPipe: %v", err)
return return
} }
// FIXME: check timeout // FIXME: check timeout
if err = cmd.Start(); err != nil { if err = cmd.Start(); err != nil {
log.Error(3, "Start: %v", err) log.Error(3, "SSH: Start: %v", err)
return return
} }
@ -97,7 +97,7 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
io.Copy(ch.Stderr(), stderr) io.Copy(ch.Stderr(), stderr)
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
log.Error(3, "Wait: %v", err) log.Error(3, "SSH: Wait: %v", err)
return return
} }
@ -119,21 +119,34 @@ func listen(config *ssh.ServerConfig, port int) {
// Once a ServerConfig has been configured, connections can be accepted. // Once a ServerConfig has been configured, connections can be accepted.
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Error(3, "Error accepting incoming connection: %v", err) log.Error(3, "SSH: Error accepting incoming connection: %v", err)
continue
}
// Before use, a handshake must be performed on the incoming net.Conn.
sConn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
log.Error(3, "Error on handshaking: %v", err)
continue continue
} }
log.Trace("Connection from %s (%s)", sConn.RemoteAddr(), sConn.ClientVersion()) // Before use, a handshake must be performed on the incoming net.Conn.
// The incoming Request channel must be serviced. // It must be handled in a separate goroutine,
go ssh.DiscardRequests(reqs) // otherwise one user could easily block entire loop.
go handleServerConn(sConn.Permissions.Extensions["key-id"], chans) // For example, user could be asked to trust server key fingerprint and hangs.
go func() {
log.Trace("SSH: Handshaking for %s", conn.RemoteAddr())
sConn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
if err == io.EOF {
log.Warn("SSH: Handshaking was terminated: %v", err)
} else {
log.Error(3, "SSH: Error on handshaking: %v", err)
}
return
}
log.Trace("SSH: Connection from %s (%s)", sConn.RemoteAddr(), sConn.ClientVersion())
// The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs)
go handleServerConn(sConn.Permissions.Extensions["key-id"], chans)
}()
} }
panic("SSH: Unexpected exit of listen loop")
} }
// Listen starts a SSH server listens on given port. // Listen starts a SSH server listens on given port.
@ -156,16 +169,16 @@ func Listen(port int) {
if err != nil { if err != nil {
panic(fmt.Sprintf("Fail to generate private key: %v - %s", err, stderr)) panic(fmt.Sprintf("Fail to generate private key: %v - %s", err, stderr))
} }
log.Trace("New private key is generateed: %s", keyPath) log.Trace("SSH: New private key is generateed: %s", keyPath)
} }
privateBytes, err := ioutil.ReadFile(keyPath) privateBytes, err := ioutil.ReadFile(keyPath)
if err != nil { if err != nil {
panic("Fail to load private key") panic("SSH: Fail to load private key")
} }
private, err := ssh.ParsePrivateKey(privateBytes) private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil { if err != nil {
panic("Fail to parse private key") panic("SSH: Fail to parse private key")
} }
config.AddHostKey(private) config.AddHostKey(private)

View File

@ -1 +1 @@
0.8.26.0131 0.8.26.0201