Skip to content

Commit fa0b93d

Browse files
committed
Prevent command line pollution from allowing privilege escalation
See matching commit in Superuser repo
1 parent ad1151e commit fa0b93d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

db.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,45 @@
2323
int database_check(const struct su_context *ctx)
2424
{
2525
FILE *fp;
26-
int allow = '-';
2726
char filename[PATH_MAX];
27+
char allow[7];
28+
int last = 0;
2829

2930
snprintf(filename, sizeof(filename),
3031
REQUESTOR_STORED_PATH "/%u-%u", ctx->from.uid, ctx->to.uid);
3132
if ((fp = fopen(filename, "r"))) {
3233
LOGD("Found file %s", filename);
34+
35+
fgets(allow, sizeof(allow), fp);
36+
last = strlen(allow) - 1;
37+
if (last >= 0)
38+
allow[last] = 0;
39+
3340
char cmd[ARG_MAX];
3441
fgets(cmd, sizeof(cmd), fp);
3542
/* skip trailing '\n' */
36-
int last = strlen(cmd) - 1;
43+
last = strlen(cmd) - 1;
3744
if (last >= 0)
3845
cmd[last] = 0;
3946

4047
LOGD("Comparing '%s' to '%s'", cmd, get_command(&ctx->to));
41-
if (strcmp(cmd, get_command(&ctx->to)) == 0) {
42-
allow = fgetc(fp);
48+
if (strcmp(cmd, get_command(&ctx->to)) != 0) {
49+
strcpy(allow, "prompt");
4350
}
4451
fclose(fp);
4552
} else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) {
4653
LOGD("Using default file %s", REQUESTOR_STORED_DEFAULT);
47-
char cmd[ARG_MAX];
48-
fgets(cmd, sizeof(cmd), fp);
49-
int last = strlen(cmd) - 1;
50-
if (last >= 0)
51-
cmd[last] = 0;
52-
53-
if (strcmp(cmd, "default") == 0) {
54-
allow = fgetc(fp);
55-
}
54+
fgets(allow, sizeof(allow), fp);
55+
last = strlen(allow) - 1;
56+
if (last >=0)
57+
allow[last] = 0;
58+
5659
fclose(fp);
5760
}
5861

59-
if (allow == '1') {
62+
if (strcmp(allow, "allow") == 0) {
6063
return ALLOW;
61-
} else if (allow == '0') {
64+
} else if (strcmp(allow, "deny") == 0) {
6265
return DENY;
6366
} else {
6467
return INTERACTIVE;

0 commit comments

Comments
 (0)