Skip to content

Commit 122bf08

Browse files
MegaManSecmillert
authored andcommitted
sudoers: digest_matches: apply runchroot before open()
Move runchroot prefixing ahead of open() so the fd and path refer to the same file. Fixes false digest matches or mismatches when fd == -1 and runchroot != NULL, since sudo_filedigest reads from fd while messages use the path. Also reduces path confusion noted by the symlink/.. comment.
1 parent 1f3dbcd commit 122bf08

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

plugins/sudoers/match_digest.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ digest_matches(int fd, const char *path, const char *runchroot,
5353
debug_return_int(ALLOW);
5454
}
5555

56-
if (fd == -1) {
57-
fd2 = open(path, O_RDONLY|O_NONBLOCK);
58-
if (fd2 == -1) {
59-
/* No file, no match. */
60-
goto done;
61-
}
62-
fd = fd2;
56+
if (runchroot != NULL) {
57+
/* XXX - handle symlinks and '..' in path outside chroot */
58+
const int len =
59+
snprintf(pathbuf, sizeof(pathbuf), "%s%s", runchroot, path);
60+
if (len >= ssizeof(pathbuf)) {
61+
errno = ENAMETOOLONG;
62+
sudo_warn("%s%s", runchroot, path);
63+
goto done;
64+
}
65+
path = pathbuf;
6366
}
6467

65-
if (runchroot != NULL) {
66-
/* XXX - handle symlinks and '..' in path outside chroot */
67-
const int len =
68-
snprintf(pathbuf, sizeof(pathbuf), "%s%s", runchroot, path);
69-
if (len >= ssizeof(pathbuf)) {
70-
errno = ENAMETOOLONG;
71-
sudo_warn("%s%s", runchroot, path);
72-
goto done;
73-
}
74-
path = pathbuf;
68+
if (fd == -1) {
69+
fd2 = open(path, O_RDONLY|O_NONBLOCK);
70+
if (fd2 == -1) {
71+
/* No file, no match. */
72+
goto done;
73+
}
74+
fd = fd2;
7575
}
7676

7777
TAILQ_FOREACH(digest, digests, entries) {

0 commit comments

Comments
 (0)