patch-svr-authpubkey_c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. --- dropbear-2020.81.orig/svr-authpubkey.c 2020-10-29 14:35:50.000000000 +0100
  2. +++ dropbear-2020.81/svr-authpubkey.c 2020-11-04 03:14:22.641017199 +0100
  3. @@ -386,26 +386,32 @@ static int checkpubkey(const char* keyal
  4. goto out;
  5. }
  6. - /* we don't need to check pw and pw_dir for validity, since
  7. - * its been done in checkpubkeyperms. */
  8. - len = strlen(ses.authstate.pw_dir);
  9. - /* allocate max required pathname storage,
  10. - * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  11. - filename = m_malloc(len + 22);
  12. - snprintf(filename, len + 22, "%s/.ssh/authorized_keys",
  13. - ses.authstate.pw_dir);
  14. + /* special case for root authorized_keys in /etc/dropbear/authorized_keys */
  15. + if (ses.authstate.pw_uid != 0) {
  16. + /* we don't need to check pw and pw_dir for validity, since
  17. + * its been done in checkpubkeyperms. */
  18. + len = strlen(ses.authstate.pw_dir);
  19. + /* allocate max required pathname storage,
  20. + * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  21. + filename = m_malloc(len + 22);
  22. + snprintf(filename, len + 22, "%s/.ssh/authorized_keys",
  23. + ses.authstate.pw_dir);
  24. -#if DROPBEAR_SVR_MULTIUSER
  25. - /* open the file as the authenticating user. */
  26. - origuid = getuid();
  27. - origgid = getgid();
  28. - if ((setegid(ses.authstate.pw_gid)) < 0 ||
  29. - (seteuid(ses.authstate.pw_uid)) < 0) {
  30. - dropbear_exit("Failed to set euid");
  31. - }
  32. -#endif
  33. + /* open the file as the authenticating user. */
  34. + origuid = getuid();
  35. + origgid = getgid();
  36. + if ((setegid(ses.authstate.pw_gid)) < 0 ||
  37. + (seteuid(ses.authstate.pw_uid)) < 0) {
  38. + dropbear_exit("Failed to set euid");
  39. + }
  40. - authfile = fopen(filename, "r");
  41. + authfile = fopen(filename, "r");
  42. +
  43. + } else {
  44. + origuid = getuid();
  45. + origgid = getgid();
  46. + authfile = fopen("/etc/dropbear/authorized_keys","r");
  47. + }
  48. #if DROPBEAR_SVR_MULTIUSER
  49. if ((seteuid(origuid)) < 0 ||
  50. @@ -474,27 +480,37 @@ static int checkpubkeyperms() {
  51. goto out;
  52. }
  53. - /* allocate max required pathname storage,
  54. - * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  55. - len += 22;
  56. - filename = m_malloc(len);
  57. - strlcpy(filename, ses.authstate.pw_dir, len);
  58. + if (ses.authstate.pw_uid != 0) {
  59. + /* allocate max required pathname storage,
  60. + * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */
  61. + filename = m_malloc(len + 22);
  62. + strncpy(filename, ses.authstate.pw_dir, len+1);
  63. +
  64. + /* check ~ */
  65. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  66. + goto out;
  67. + }
  68. - /* check ~ */
  69. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  70. - goto out;
  71. - }
  72. + /* check ~/.ssh */
  73. + strncat(filename, "/.ssh", 5); /* strlen("/.ssh") == 5 */
  74. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  75. + goto out;
  76. + }
  77. - /* check ~/.ssh */
  78. - strlcat(filename, "/.ssh", len);
  79. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  80. - goto out;
  81. - }
  82. + /* now check ~/.ssh/authorized_keys */
  83. + strncat(filename, "/authorized_keys", 16);
  84. + if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  85. + goto out;
  86. + }
  87. - /* now check ~/.ssh/authorized_keys */
  88. - strlcat(filename, "/authorized_keys", len);
  89. - if (checkfileperm(filename) != DROPBEAR_SUCCESS) {
  90. - goto out;
  91. + } else {
  92. +
  93. + if (checkfileperm("/etc/dropbear") != DROPBEAR_SUCCESS) {
  94. + goto out;
  95. + }
  96. + if (checkfileperm("/etc/dropbear/authorized_keys") != DROPBEAR_SUCCESS) {
  97. + goto out;
  98. + }
  99. }
  100. /* file looks ok, return success */