Skip to content

Commit daaa560

Browse files
bpf: introduce MAX_POSSIBLE_ARGS_MASK
Use it for masking to appease the verifier when we have arrays with MAX_POSSIBLE_ARGS. This prevents silent breakage if we increase the size of MAX_POSSIBLE_ARGS beyond 8 in the future. Suggested-by: Kornilios Kourtis <kornilios@isovalent.com> Signed-off-by: Andy Strohman <astrohma@isovalent.com>
1 parent efae519 commit daaa560

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

bpf/lib/generic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#define SELECTORS_ACTIVE 31
2222
#define MAX_CONFIGURED_SELECTORS MAX_POSSIBLE_SELECTORS + 1
2323

24+
/* convenience mask for verifier appeasing*/
25+
#define MAX_POSSIBLE_ARGS_MASK 0x7
26+
_Static_assert(MAX_POSSIBLE_ARGS - 1 <= MAX_POSSIBLE_ARGS_MASK, "Need to update MAX_POSSIBLE_ARGS_MASK");
27+
2428
struct msg_selector_data {
2529
__u64 curr;
2630
bool pass;

bpf/process/generic_calls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ read_arg(void *ctx, int index, int type, long orig_off, unsigned long arg, int a
454454

455455
orig_off &= 16383;
456456

457-
index &= MAX_SELECTORS_MASK;
457+
index &= MAX_POSSIBLE_ARGS_MASK;
458458
orig_off = write_arg_status(e, orig_off, e->arg_status[index]);
459459
/* Cache args offset for filter use later */
460460
e->argsoff[index] = orig_off;

bpf/process/generic_path.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ FUNC_INLINE long generic_path_offload(void *ctx, long ty, unsigned long arg,
199199
if (!buffer)
200200
return 0;
201201

202-
index &= MAX_SELECTORS_MASK;
202+
index &= MAX_POSSIBLE_ARGS_MASK;
203203

204204
orig_off = write_arg_status(e, orig_off, e->arg_status[index]);
205205

bpf/process/types/basic.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,9 @@ get_arg(struct msg_generic_kprobe *e, __u32 index)
19221922
{
19231923
long argoff;
19241924

1925-
asm volatile("%[index] &= 0x7;\n" : [index] "+r"(index));
1925+
asm volatile("%[index] &= %[mask];\n"
1926+
: [index] "+r"(index)
1927+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
19261928
argoff = e->argsoff[index];
19271929
asm volatile("%[argoff] &= 0x7ff;\n" : [argoff] "+r"(argoff));
19281930
return &e->args[argoff];
@@ -1967,7 +1969,9 @@ filter_arg_1(struct msg_generic_kprobe *e, struct selector_arg_filter *filter, c
19671969
__u64 cap_old = *(__u64 *)args;
19681970
__u32 index2 = *((__u32 *)&filter->value);
19691971

1970-
asm volatile("%[index2] &= 0x7;\n" : [index2] "+r"(index2));
1972+
asm volatile("%[index2] &= %[mask];\n"
1973+
: [index2] "+r"(index2)
1974+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
19711975
if (!is_arg_ok(e, index2))
19721976
return 0;
19731977
__u64 cap_new = *(__u64 *)get_arg(e, index2);
@@ -2168,12 +2172,11 @@ installfd(struct msg_generic_kprobe *e, int fd, int name, bool follow)
21682172
/* Satisfies verifier but is a bit ugly, ideally we
21692173
* can just '&' and drop the '>' case.
21702174
*/
2171-
asm volatile("%[fd] &= 0xf;\n"
2175+
asm volatile("%[fd] &= %[mask];\n"
21722176
: [fd] "+r"(fd)
2173-
:);
2174-
if (fd > 5) {
2177+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
2178+
if (fd > MAX_POSSIBLE_ARGS)
21752179
return 0;
2176-
}
21772180

21782181
if (!is_arg_ok(e, fd))
21792182
return 0;
@@ -2189,10 +2192,10 @@ installfd(struct msg_generic_kprobe *e, int fd, int name, bool follow)
21892192
if (follow) {
21902193
__u32 size;
21912194

2192-
asm volatile("%[name] &= 0xf;\n"
2195+
asm volatile("%[name] &= %[mask];\n"
21932196
: [name] "+r"(name)
2194-
:);
2195-
if (name > 5)
2197+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
2198+
if (name > MAX_POSSIBLE_ARGS)
21962199
return 0;
21972200

21982201
if (!is_arg_ok(e, name))
@@ -2244,10 +2247,10 @@ copyfd(struct msg_generic_kprobe *e, int oldfd, int newfd)
22442247
int oldfdoff, newfdoff;
22452248
int err = 0;
22462249

2247-
asm volatile("%[oldfd] &= 0xf;\n"
2250+
asm volatile("%[oldfd] &= %[mask];\n"
22482251
: [oldfd] "+r"(oldfd)
2249-
:);
2250-
if (oldfd > 5)
2252+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
2253+
if (oldfd > MAX_POSSIBLE_ARGS)
22512254
return 0;
22522255
if (!is_arg_ok(e, oldfd))
22532256
return 0;
@@ -2261,10 +2264,10 @@ copyfd(struct msg_generic_kprobe *e, int oldfd, int newfd)
22612264

22622265
val = map_lookup_elem(&fdinstall_map, &key);
22632266
if (val) {
2264-
asm volatile("%[newfd] &= 0xf;\n"
2267+
asm volatile("%[newfd] &= %[mask];\n"
22652268
: [newfd] "+r"(newfd)
2266-
:);
2267-
if (newfd > 5)
2269+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
2270+
if (newfd > MAX_POSSIBLE_ARGS)
22682271
return 0;
22692272
if (!is_arg_ok(e, newfd))
22702273
return 0;
@@ -2401,10 +2404,10 @@ tracksock(struct msg_generic_kprobe *e, int socki, bool track)
24012404
/* Satisfies verifier but is a bit ugly, ideally we
24022405
* can just '&' and drop the '>' case.
24032406
*/
2404-
asm volatile("%[socki] &= 0xf;\n"
2407+
asm volatile("%[socki] &= %[mask];\n"
24052408
: [socki] "+r"(socki)
2406-
:);
2407-
if (socki > 5)
2409+
: [mask] "i"(MAX_POSSIBLE_ARGS_MASK));
2410+
if (socki > MAX_POSSIBLE_ARGS)
24082411
return 0;
24092412

24102413
if (!is_arg_ok(e, socki))

0 commit comments

Comments
 (0)