-
Notifications
You must be signed in to change notification settings - Fork 293
unixpwd: fix reentrance of unixpwd_setpwd() and unixpwd_setspw() #6729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,7 +86,8 @@ unixpwd_setpwd(const char *user, char *password) | |
| *pw; | ||
| char buf[BUFLEN]; | ||
| int tmp; | ||
| FILE *tmp_file; | ||
| FILE *tmp_file, | ||
| *passwd_file; | ||
| char tmp_name[PATH_MAX]; | ||
| struct stat statbuf; | ||
| int rc; | ||
|
|
@@ -96,18 +97,28 @@ unixpwd_setpwd(const char *user, char *password) | |
| tmp = mkstemp(tmp_name); | ||
| if (tmp == -1) | ||
| return errno; | ||
| if (stat(ETC_PASSWD, &statbuf) != 0 | ||
|
|
||
| passwd_file = fopen(ETC_PASSWD, "rbe"); | ||
| if (passwd_file == NULL) { | ||
| rc = errno; | ||
| close(tmp); | ||
| unlink(tmp_name); | ||
| return rc; | ||
| } | ||
|
|
||
| if (fstat(fileno(passwd_file), &statbuf) != 0 | ||
| || fchown(tmp, statbuf.st_uid, statbuf.st_gid) != 0 | ||
| || fchmod(tmp, statbuf.st_mode) != 0 | ||
| || (tmp_file = fdopen(tmp, "w")) == NULL) { | ||
| rc = errno ? errno : EPERM; | ||
| fclose(passwd_file); | ||
| close(tmp); | ||
| unlink(tmp_name); | ||
| return rc; | ||
| } | ||
|
|
||
| setpwent(); | ||
| while (1) { | ||
| rc = getpwent_r(&pwd, buf, BUFLEN, &pw); | ||
| rc = fgetpwent_r(passwd_file, &pwd, buf, BUFLEN, &pw); | ||
| if (rc != 0 || !pw) | ||
| break; | ||
| if (!strcmp(user, pw->pw_name)) { | ||
|
|
@@ -116,8 +127,8 @@ unixpwd_setpwd(const char *user, char *password) | |
| } | ||
| putpwent(pw, tmp_file); | ||
| } | ||
| endpwent(); | ||
|
|
||
| fclose(passwd_file); | ||
| fclose(tmp_file); | ||
| if (rc != ENOENT) { | ||
| unlink(tmp_name); | ||
|
|
@@ -144,7 +155,8 @@ unixpwd_setspw(const char *user, char *password) | |
| *sp; | ||
| char buf[BUFLEN]; | ||
| int tmp; | ||
| FILE *tmp_file = NULL; | ||
| FILE *tmp_file = NULL, | ||
| *spasswd_file; | ||
| char tmp_name[PATH_MAX]; | ||
| struct stat statbuf; | ||
| int rc; | ||
|
|
@@ -154,26 +166,37 @@ unixpwd_setspw(const char *user, char *password) | |
| tmp = mkstemp(tmp_name); | ||
| if (tmp == -1) | ||
| return errno; | ||
| if (stat(ETC_SPASSWD, &statbuf) != 0 | ||
|
|
||
| spasswd_file = fopen(ETC_SPASSWD, "rbe"); | ||
| if (spasswd_file == NULL) { | ||
| rc = errno; | ||
| close(tmp); | ||
| unlink(tmp_name); | ||
| return rc; | ||
| } | ||
|
|
||
| if (fstat(fileno(spasswd_file), &statbuf) != 0 | ||
| || fchown(tmp, statbuf.st_uid, statbuf.st_gid) != 0 | ||
| || fchmod(tmp, statbuf.st_mode) != 0 | ||
| || (tmp_file = fdopen(tmp, "w")) == NULL) { | ||
| rc = errno ? errno : EPERM; | ||
| fclose(spasswd_file); | ||
| close(tmp); | ||
| unlink(tmp_name); | ||
| return rc; | ||
| } | ||
| if (lckpwdf() != 0) { | ||
| fclose(spasswd_file); | ||
| if (tmp_file) | ||
| fclose(tmp_file); | ||
| close(tmp); | ||
| else | ||
| close(tmp); | ||
| unlink(tmp_name); | ||
| return ENOLCK; | ||
| } | ||
|
|
||
| setspent(); | ||
| while (1) { | ||
| rc = getspent_r(&spw, buf, BUFLEN, &sp); | ||
| rc = fgetspent_r(spasswd_file, &spw, buf, BUFLEN, &sp); | ||
| if (rc != 0 || !sp) | ||
| break; | ||
| if (!strcmp(user, sp->sp_namp)) { | ||
|
|
@@ -182,8 +205,8 @@ unixpwd_setspw(const char *user, char *password) | |
| } | ||
| putspent(sp, tmp_file); | ||
| } | ||
| endspent(); | ||
|
|
||
| fclose(spasswd_file); | ||
| fclose(tmp_file); | ||
| if (rc != ENOENT) { | ||
| ulckpwdf(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is still global, right? (If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About Ocaml lock, yes, it should be released in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, but then we need to introduce another mutex here (which could still be beneficial, because it'd allow other OCaml threads to keep running, while this one deals with the password database, which is probably quite rare) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although if the intention is just to get this to compile with Musl (#6729 (comment)), then keeping the OCaml runtime lock as is might be simpler (we don't gain any additional concurrency, and we don't risk introducing any concurrency bugs in our existing code either). |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.