patch-svr-authpubkey_c 3.3 KB

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