Skip to content

Commit 1d30b3f

Browse files
RyanMallonDenys Vlasenko
authored and
Denys Vlasenko
committed
wall,crontab: use xopen_as_uid_gid()
This fixes a narrow security race in crontab. function old new delta xopen_as_uid_gid - 80 +80 seteuid - 64 +64 setegid - 64 +64 setreuid - 37 +37 xseteuid - 22 +22 xsetegid - 22 +22 crontab_main 590 577 -13 setfsuid 33 - -33 setfsgid 33 - -33 wall_main 138 102 -36 open_as_user 109 - -109 text data bss dec hex filename 893539 497 7568 901604 dc1e4 busybox_old 893618 497 7568 901683 dc233 busybox_unstripped Signed-off-by: Ryan Mallon <[email protected]> Signed-off-by: Denys Vlasenko <[email protected]>
1 parent 5906a5c commit 1d30b3f

File tree

2 files changed

+2
-31
lines changed

2 files changed

+2
-31
lines changed

Diff for: miscutils/crontab.c

+1-26
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,6 @@ static void edit_file(const struct passwd *pas, const char *file)
5555
bb_perror_msg_and_die("can't execute '%s'", ptr);
5656
}
5757

58-
static int open_as_user(const struct passwd *pas, const char *file)
59-
{
60-
pid_t pid;
61-
char c;
62-
63-
pid = xvfork();
64-
if (pid) { /* PARENT */
65-
if (wait4pid(pid) == 0) {
66-
/* exitcode 0: child says it can read */
67-
return open(file, O_RDONLY);
68-
}
69-
return -1;
70-
}
71-
72-
/* CHILD */
73-
/* initgroups, setgid, setuid */
74-
change_identity(pas);
75-
/* We just try to read one byte. If it works, file is readable
76-
* under this user. We signal that by exiting with 0. */
77-
_exit(safe_read(xopen(file, O_RDONLY), &c, 1) < 0);
78-
}
79-
8058
int crontab_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
8159
int crontab_main(int argc UNUSED_PARAM, char **argv)
8260
{
@@ -137,10 +115,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
137115
if (!argv[0])
138116
bb_show_usage();
139117
if (NOT_LONE_DASH(argv[0])) {
140-
src_fd = open_as_user(pas, argv[0]);
141-
if (src_fd < 0)
142-
bb_error_msg_and_die("user %s cannot read %s",
143-
pas->pw_name, argv[0]);
118+
src_fd = xopen_as_uid_gid(argv[0], O_RDONLY, pas->pw_uid, pas->pw_gid);
144119
}
145120
}
146121

Diff for: miscutils/wall.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ int wall_main(int argc UNUSED_PARAM, char **argv)
4141
/* The applet is setuid.
4242
* Access to the file must be under user's uid/gid.
4343
*/
44-
setfsuid(getuid());
45-
setfsgid(getgid());
46-
fd = xopen(argv[1], O_RDONLY);
47-
setfsuid(geteuid());
48-
setfsgid(getegid());
44+
fd = xopen_as_uid_gid(argv[1], O_RDONLY, getuid(), getgid());
4945
}
5046
msg = xmalloc_read(fd, NULL);
5147
if (ENABLE_FEATURE_CLEAN_UP && argv[1])

0 commit comments

Comments
 (0)