Skip to content

Commit ff52209

Browse files
Tao ChenKernel Patches Daemon
authored andcommitted
bpftool: Fix UAF in get_delegate_value
The return value ret pointer is pointing opts_copy, but opts_copy gets freed in get_delegate_value before return, fix this by strdup a new buffer. Fixes: 2d81231 ("bpftool: Add bpf_token show") Signed-off-by: Tao Chen <[email protected]>
1 parent 4941ed0 commit ff52209

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

tools/bpf/bpftool/token.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ static bool has_delegate_options(const char *mnt_ops)
2828
strstr(mnt_ops, "delegate_attachs");
2929
}
3030

31+
static void free_delegate_value(char *value)
32+
{
33+
if (value)
34+
free(value);
35+
}
36+
3137
static char *get_delegate_value(const char *opts, const char *key)
3238
{
3339
char *token, *rest, *ret = NULL;
@@ -40,7 +46,7 @@ static char *get_delegate_value(const char *opts, const char *key)
4046
token = strtok_r(NULL, ",", &rest)) {
4147
if (strncmp(token, key, strlen(key)) == 0 &&
4248
token[strlen(key)] == '=') {
43-
ret = token + strlen(key) + 1;
49+
ret = strdup(token + strlen(key) + 1);
4450
break;
4551
}
4652
}
@@ -73,28 +79,29 @@ static void print_items_per_line(const char *input, int items_per_line)
7379
free(strs);
7480
}
7581

82+
#define PRINT_DELEGATE_OPT(opt_name) do { \
83+
char *value = get_delegate_value(mntent->mnt_opts, opt_name); \
84+
print_items_per_line(value, ITEMS_PER_LINE); \
85+
free_delegate_value(value); \
86+
} while (0)
87+
7688
#define ITEMS_PER_LINE 4
7789
static void show_token_info_plain(struct mntent *mntent)
7890
{
79-
char *value;
8091

8192
printf("token_info %s", mntent->mnt_dir);
8293

8394
printf("\n\tallowed_cmds:");
84-
value = get_delegate_value(mntent->mnt_opts, "delegate_cmds");
85-
print_items_per_line(value, ITEMS_PER_LINE);
95+
PRINT_DELEGATE_OPT("delegate_cmds");
8696

8797
printf("\n\tallowed_maps:");
88-
value = get_delegate_value(mntent->mnt_opts, "delegate_maps");
89-
print_items_per_line(value, ITEMS_PER_LINE);
98+
PRINT_DELEGATE_OPT("delegate_maps");
9099

91100
printf("\n\tallowed_progs:");
92-
value = get_delegate_value(mntent->mnt_opts, "delegate_progs");
93-
print_items_per_line(value, ITEMS_PER_LINE);
101+
PRINT_DELEGATE_OPT("delegate_progs");
94102

95103
printf("\n\tallowed_attachs:");
96-
value = get_delegate_value(mntent->mnt_opts, "delegate_attachs");
97-
print_items_per_line(value, ITEMS_PER_LINE);
104+
PRINT_DELEGATE_OPT("delegate_attachs");
98105
printf("\n");
99106
}
100107

@@ -122,29 +129,29 @@ static void split_json_array_str(const char *input)
122129
free(strs);
123130
}
124131

132+
#define PRINT_DELEGATE_OPT_JSON(opt_name) do { \
133+
char *value = get_delegate_value(mntent->mnt_opts, opt_name); \
134+
split_json_array_str(value); \
135+
free_delegate_value(value); \
136+
} while (0)
137+
125138
static void show_token_info_json(struct mntent *mntent)
126139
{
127-
char *value;
128-
129140
jsonw_start_object(json_wtr);
130141

131142
jsonw_string_field(json_wtr, "token_info", mntent->mnt_dir);
132143

133144
jsonw_name(json_wtr, "allowed_cmds");
134-
value = get_delegate_value(mntent->mnt_opts, "delegate_cmds");
135-
split_json_array_str(value);
145+
PRINT_DELEGATE_OPT_JSON("delegate_cmds");
136146

137147
jsonw_name(json_wtr, "allowed_maps");
138-
value = get_delegate_value(mntent->mnt_opts, "delegate_maps");
139-
split_json_array_str(value);
148+
PRINT_DELEGATE_OPT_JSON("delegate_maps");
140149

141150
jsonw_name(json_wtr, "allowed_progs");
142-
value = get_delegate_value(mntent->mnt_opts, "delegate_progs");
143-
split_json_array_str(value);
151+
PRINT_DELEGATE_OPT_JSON("delegate_progs");
144152

145153
jsonw_name(json_wtr, "allowed_attachs");
146-
value = get_delegate_value(mntent->mnt_opts, "delegate_attachs");
147-
split_json_array_str(value);
154+
PRINT_DELEGATE_OPT_JSON("delegate_attachs");
148155

149156
jsonw_end_object(json_wtr);
150157
}

0 commit comments

Comments
 (0)