gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package ssh
  4. import (
  5. "bytes"
  6. "context"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "encoding/pem"
  11. "errors"
  12. "io"
  13. "net"
  14. "os"
  15. "os/exec"
  16. "path/filepath"
  17. "reflect"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "syscall"
  22. asymkey_model "code.gitea.io/gitea/models/asymkey"
  23. "code.gitea.io/gitea/modules/graceful"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/process"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/util"
  28. "github.com/gliderlabs/ssh"
  29. gossh "golang.org/x/crypto/ssh"
  30. )
  31. // The ssh auth overall works like this:
  32. // NewServerConn:
  33. // serverHandshake+serverAuthenticate:
  34. // PublicKeyCallback:
  35. // PublicKeyHandler (our code):
  36. // reset(ctx.Permissions) and set ctx.Permissions.giteaKeyID = keyID
  37. // pubKey.Verify
  38. // return ctx.Permissions // only reaches here, the pub key is really authenticated
  39. // set conn.Permissions from serverAuthenticate
  40. // sessionHandler(conn)
  41. //
  42. // Then sessionHandler should only use the "verified keyID" from the original ssh conn, but not the ctx one.
  43. // Otherwise, if a user provides 2 keys A (a correct one) and B (public key matches but no private key),
  44. // then only A succeeds to authenticate, sessionHandler will see B's keyID
  45. //
  46. // After x/crypto >= 0.31.0 (fix CVE-2024-45337), the PublicKeyCallback will be called again for the verified key,
  47. // it mitigates the misuse for most cases, it's still good for us to make sure we don't rely on that mitigation
  48. // and do not misuse the PublicKeyCallback: we should only use the verified keyID from the verified ssh conn.
  49. const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
  50. func getExitStatusFromError(err error) int {
  51. if err == nil {
  52. return 0
  53. }
  54. exitErr, ok := err.(*exec.ExitError)
  55. if !ok {
  56. return 1
  57. }
  58. waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
  59. if !ok {
  60. // This is a fallback and should at least let us return something useful
  61. // when running on Windows, even if it isn't completely accurate.
  62. if exitErr.Success() {
  63. return 0
  64. }
  65. return 1
  66. }
  67. return waitStatus.ExitStatus()
  68. }
  69. // sessionPartial is the private struct from "gliderlabs/ssh/session.go"
  70. // We need to read the original "conn" field from "ssh.Session interface" which contains the "*session pointer"
  71. // https://github.com/gliderlabs/ssh/blob/d137aad99cd6f2d9495bfd98c755bec4e5dffb8c/session.go#L109-L113
  72. // If upstream fixes the problem and/or changes the struct, we need to follow.
  73. // If the struct mismatches, the builtin ssh server will fail during integration tests.
  74. type sessionPartial struct {
  75. sync.Mutex
  76. gossh.Channel
  77. conn *gossh.ServerConn
  78. }
  79. func ptr[T any](intf any) *T {
  80. // https://pkg.go.dev/unsafe#Pointer
  81. // (1) Conversion of a *T1 to Pointer to *T2.
  82. // Provided that T2 is no larger than T1 and that the two share an equivalent memory layout,
  83. // this conversion allows reinterpreting data of one type as data of another type.
  84. v := reflect.ValueOf(intf)
  85. p := v.UnsafePointer()
  86. return (*T)(p)
  87. }
  88. func sessionHandler(session ssh.Session) {
  89. // here can't use session.Permissions() because it only uses the value from ctx, which might not be the authenticated one.
  90. // so we must use the original ssh conn, which always contains the correct (verified) keyID.
  91. sshSession := ptr[sessionPartial](session)
  92. keyID := sshSession.conn.Permissions.Extensions[giteaPermissionExtensionKeyID]
  93. command := session.RawCommand()
  94. log.Trace("SSH: Payload: %v", command)
  95. args := []string{"--config=" + setting.CustomConf, "serv", "key-" + keyID}
  96. log.Trace("SSH: Arguments: %v", args)
  97. ctx, cancel := context.WithCancel(session.Context())
  98. defer cancel()
  99. gitProtocol := ""
  100. for _, env := range session.Environ() {
  101. if strings.HasPrefix(env, "GIT_PROTOCOL=") {
  102. _, gitProtocol, _ = strings.Cut(env, "=")
  103. break
  104. }
  105. }
  106. cmd := exec.CommandContext(ctx, setting.AppPath, args...)
  107. cmd.Env = append(
  108. os.Environ(),
  109. "SSH_ORIGINAL_COMMAND="+command,
  110. "SKIP_MINWINSVC=1",
  111. "GIT_PROTOCOL="+gitProtocol,
  112. )
  113. stdout, err := cmd.StdoutPipe()
  114. if err != nil {
  115. log.Error("SSH: StdoutPipe: %v", err)
  116. return
  117. }
  118. defer stdout.Close()
  119. stderr, err := cmd.StderrPipe()
  120. if err != nil {
  121. log.Error("SSH: StderrPipe: %v", err)
  122. return
  123. }
  124. defer stderr.Close()
  125. stdin, err := cmd.StdinPipe()
  126. if err != nil {
  127. log.Error("SSH: StdinPipe: %v", err)
  128. return
  129. }
  130. defer stdin.Close()
  131. process.SetSysProcAttribute(cmd)
  132. wg := &sync.WaitGroup{}
  133. wg.Add(2)
  134. if err = cmd.Start(); err != nil {
  135. log.Error("SSH: Start: %v", err)
  136. return
  137. }
  138. go func() {
  139. defer stdin.Close()
  140. if _, err := io.Copy(stdin, session); err != nil {
  141. log.Error("Failed to write session to stdin. %s", err)
  142. }
  143. }()
  144. go func() {
  145. defer wg.Done()
  146. defer stdout.Close()
  147. if _, err := io.Copy(session, stdout); err != nil {
  148. log.Error("Failed to write stdout to session. %s", err)
  149. }
  150. }()
  151. go func() {
  152. defer wg.Done()
  153. defer stderr.Close()
  154. if _, err := io.Copy(session.Stderr(), stderr); err != nil {
  155. log.Error("Failed to write stderr to session. %s", err)
  156. }
  157. }()
  158. // Ensure all the output has been written before we wait on the command
  159. // to exit.
  160. wg.Wait()
  161. // Wait for the command to exit and log any errors we get
  162. err = cmd.Wait()
  163. if err != nil {
  164. // Cannot use errors.Is here because ExitError doesn't implement Is
  165. // Thus errors.Is will do equality test NOT type comparison
  166. if _, ok := err.(*exec.ExitError); !ok {
  167. log.Error("SSH: Wait: %v", err)
  168. }
  169. }
  170. if err := session.Exit(getExitStatusFromError(err)); err != nil && !errors.Is(err, io.EOF) {
  171. log.Error("Session failed to exit. %s", err)
  172. }
  173. }
  174. func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
  175. // The publicKeyHandler (PublicKeyCallback) only helps to provide the candidate keys to authenticate,
  176. // It does NOT really verify here, so we could only record the related information here.
  177. // After authentication (Verify), the "Permissions" will be assigned to the ssh conn,
  178. // then we can use it in the "session handler"
  179. // first, reset the ctx permissions (just like https://github.com/gliderlabs/ssh/pull/243 does)
  180. // it shouldn't be reused across different ssh conn (sessions), each pub key should have its own "Permissions"
  181. ctx.Permissions().Permissions = &gossh.Permissions{}
  182. setPermExt := func(keyID int64) {
  183. ctx.Permissions().Permissions.Extensions = map[string]string{
  184. giteaPermissionExtensionKeyID: strconv.FormatInt(keyID, 10),
  185. }
  186. }
  187. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  188. log.Debug("Handle Public Key: Fingerprint: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr())
  189. }
  190. if ctx.User() != setting.SSH.BuiltinServerUser {
  191. log.Warn("Invalid SSH username %s - must use %s for all git operations via ssh", ctx.User(), setting.SSH.BuiltinServerUser)
  192. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  193. return false
  194. }
  195. // check if we have a certificate
  196. if cert, ok := key.(*gossh.Certificate); ok {
  197. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  198. log.Debug("Handle Certificate: %s Fingerprint: %s is a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  199. }
  200. if len(setting.SSH.TrustedUserCAKeys) == 0 {
  201. log.Warn("Certificate Rejected: No trusted certificate authorities for this server")
  202. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  203. return false
  204. }
  205. if cert.CertType != gossh.UserCert {
  206. log.Warn("Certificate Rejected: Not a user certificate")
  207. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  208. return false
  209. }
  210. // look for the exact principal
  211. principalLoop:
  212. for _, principal := range cert.ValidPrincipals {
  213. pkey, err := asymkey_model.SearchPublicKeyByContentExact(ctx, principal)
  214. if err != nil {
  215. if asymkey_model.IsErrKeyNotExist(err) {
  216. log.Debug("Principal Rejected: %s Unknown Principal: %s", ctx.RemoteAddr(), principal)
  217. continue principalLoop
  218. }
  219. log.Error("SearchPublicKeyByContentExact: %v", err)
  220. return false
  221. }
  222. c := &gossh.CertChecker{
  223. IsUserAuthority: func(auth gossh.PublicKey) bool {
  224. marshaled := auth.Marshal()
  225. for _, k := range setting.SSH.TrustedUserCAKeysParsed {
  226. if bytes.Equal(marshaled, k.Marshal()) {
  227. return true
  228. }
  229. }
  230. return false
  231. },
  232. }
  233. // check the CA of the cert
  234. if !c.IsUserAuthority(cert.SignatureKey) {
  235. if log.IsDebug() {
  236. log.Debug("Principal Rejected: %s Untrusted Authority Signature Fingerprint %s for Principal: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(cert.SignatureKey), principal)
  237. }
  238. continue principalLoop
  239. }
  240. // validate the cert for this principal
  241. if err := c.CheckCert(principal, cert); err != nil {
  242. // User is presenting an invalid certificate - STOP any further processing
  243. log.Error("Invalid Certificate KeyID %s with Signature Fingerprint %s presented for Principal: %s from %s", cert.KeyId, gossh.FingerprintSHA256(cert.SignatureKey), principal, ctx.RemoteAddr())
  244. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  245. return false
  246. }
  247. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  248. log.Debug("Successfully authenticated: %s Certificate Fingerprint: %s Principal: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key), principal)
  249. }
  250. setPermExt(pkey.ID)
  251. return true
  252. }
  253. log.Warn("From %s Fingerprint: %s is a certificate, but no valid principals found", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  254. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  255. return false
  256. }
  257. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  258. log.Debug("Handle Public Key: %s Fingerprint: %s is not a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  259. }
  260. pkey, err := asymkey_model.SearchPublicKeyByContent(ctx, strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
  261. if err != nil {
  262. if asymkey_model.IsErrKeyNotExist(err) {
  263. log.Warn("Unknown public key: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr())
  264. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  265. return false
  266. }
  267. log.Error("SearchPublicKeyByContent: %v", err)
  268. return false
  269. }
  270. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  271. log.Debug("Successfully authenticated: %s Public Key Fingerprint: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  272. }
  273. setPermExt(pkey.ID)
  274. return true
  275. }
  276. // sshConnectionFailed logs a failed connection
  277. // - this mainly exists to give a nice function name in logging
  278. func sshConnectionFailed(conn net.Conn, err error) {
  279. // Log the underlying error with a specific message
  280. log.Warn("Failed connection from %s with error: %v", conn.RemoteAddr(), err)
  281. // Log with the standard failed authentication from message for simpler fail2ban configuration
  282. log.Warn("Failed authentication attempt from %s", conn.RemoteAddr())
  283. }
  284. // Listen starts an SSH server listening on given port.
  285. func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
  286. srv := ssh.Server{
  287. Addr: net.JoinHostPort(host, strconv.Itoa(port)),
  288. PublicKeyHandler: publicKeyHandler,
  289. Handler: sessionHandler,
  290. ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig {
  291. config := &gossh.ServerConfig{}
  292. config.KeyExchanges = keyExchanges
  293. config.MACs = macs
  294. config.Ciphers = ciphers
  295. return config
  296. },
  297. ConnectionFailedCallback: sshConnectionFailed,
  298. // We need to explicitly disable the PtyCallback so text displays
  299. // properly.
  300. PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
  301. return false
  302. },
  303. }
  304. keys := make([]string, 0, len(setting.SSH.ServerHostKeys))
  305. for _, key := range setting.SSH.ServerHostKeys {
  306. isExist, err := util.IsExist(key)
  307. if err != nil {
  308. log.Fatal("Unable to check if %s exists. Error: %v", setting.SSH.ServerHostKeys, err)
  309. }
  310. if isExist {
  311. keys = append(keys, key)
  312. }
  313. }
  314. if len(keys) == 0 {
  315. filePath := filepath.Dir(setting.SSH.ServerHostKeys[0])
  316. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  317. log.Error("Failed to create dir %s: %v", filePath, err)
  318. }
  319. err := GenKeyPair(setting.SSH.ServerHostKeys[0])
  320. if err != nil {
  321. log.Fatal("Failed to generate private key: %v", err)
  322. }
  323. log.Trace("New private key is generated: %s", setting.SSH.ServerHostKeys[0])
  324. keys = append(keys, setting.SSH.ServerHostKeys[0])
  325. }
  326. for _, key := range keys {
  327. log.Info("Adding SSH host key: %s", key)
  328. err := srv.SetOption(ssh.HostKeyFile(key))
  329. if err != nil {
  330. log.Error("Failed to set Host Key. %s", err)
  331. }
  332. }
  333. go func() {
  334. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Service: Built-in SSH server", process.SystemProcessType, true)
  335. defer finished()
  336. listen(&srv)
  337. }()
  338. }
  339. // GenKeyPair make a pair of public and private keys for SSH access.
  340. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  341. // Private Key generated is PEM encoded
  342. func GenKeyPair(keyPath string) error {
  343. privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
  344. if err != nil {
  345. return err
  346. }
  347. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  348. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  349. if err != nil {
  350. return err
  351. }
  352. defer func() {
  353. if err = f.Close(); err != nil {
  354. log.Error("Close: %v", err)
  355. }
  356. }()
  357. if err := pem.Encode(f, privateKeyPEM); err != nil {
  358. return err
  359. }
  360. // generate public key
  361. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  362. if err != nil {
  363. return err
  364. }
  365. public := gossh.MarshalAuthorizedKey(pub)
  366. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  367. if err != nil {
  368. return err
  369. }
  370. defer func() {
  371. if err = p.Close(); err != nil {
  372. log.Error("Close: %v", err)
  373. }
  374. }()
  375. _, err = p.Write(public)
  376. return err
  377. }