Skip to content

Commit bd07bd1

Browse files
James BottomleyAlexei Starovoitov
authored andcommitted
bpf: Fix key serial argument of bpf_lookup_user_key()
The underlying lookup_user_key() function uses a signed 32 bit integer for key serial numbers because legitimate serial numbers are positive (and > 3) and keyrings are negative. Using a u32 for the keyring in the bpf function doesn't currently cause any conversion problems but will start to trip the signed to unsigned conversion warnings when the kernel enables them, so convert the argument to signed (and update the tests accordingly) before it acquires more users. Signed-off-by: James Bottomley <[email protected]> Reviewed-by: Roberto Sassu <[email protected]> Link: https://lore.kernel.org/r/84cdb0775254d297d75e21f577089f64abdfbd28.camel@HansenPartnership.com Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f5527f0 commit bd07bd1

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

kernel/trace/bpf_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ __bpf_kfunc_start_defs();
12701270
* Return: a bpf_key pointer with a valid key pointer if the key is found, a
12711271
* NULL pointer otherwise.
12721272
*/
1273-
__bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1273+
__bpf_kfunc struct bpf_key *bpf_lookup_user_key(s32 serial, u64 flags)
12741274
{
12751275
key_ref_t key_ref;
12761276
struct bpf_key *bkey;

tools/testing/selftests/bpf/bpf_kfuncs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extern int bpf_get_file_xattr(struct file *file, const char *name,
6969
struct bpf_dynptr *value_ptr) __ksym;
7070
extern int bpf_get_fsverity_digest(struct file *file, struct bpf_dynptr *digest_ptr) __ksym;
7171

72-
extern struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
72+
extern struct bpf_key *bpf_lookup_user_key(__s32 serial, __u64 flags) __ksym;
7373
extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
7474
extern void bpf_key_put(struct bpf_key *key) __ksym;
7575
extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,

tools/testing/selftests/bpf/progs/rcu_read_lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ struct {
1616
__type(value, long);
1717
} map_a SEC(".maps");
1818

19-
__u32 user_data, key_serial, target_pid;
19+
__u32 user_data, target_pid;
20+
__s32 key_serial;
2021
__u64 flags, task_storage_val, cgroup_id;
2122

22-
struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
23+
struct bpf_key *bpf_lookup_user_key(__s32 serial, __u64 flags) __ksym;
2324
void bpf_key_put(struct bpf_key *key) __ksym;
2425
void bpf_rcu_read_lock(void) __ksym;
2526
void bpf_rcu_read_unlock(void) __ksym;

tools/testing/selftests/bpf/progs/test_lookup_key.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
char _license[] SEC("license") = "GPL";
1515

1616
__u32 monitored_pid;
17-
__u32 key_serial;
17+
__s32 key_serial;
1818
__u32 key_id;
1919
__u64 flags;
2020

21-
extern struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
21+
extern struct bpf_key *bpf_lookup_user_key(__s32 serial, __u64 flags) __ksym;
2222
extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
2323
extern void bpf_key_put(struct bpf_key *key) __ksym;
2424

tools/testing/selftests/bpf/progs/test_sig_in_xattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ char digest[MAGIC_SIZE + SIZEOF_STRUCT_FSVERITY_DIGEST + SHA256_DIGEST_SIZE];
4040
__u32 monitored_pid;
4141
char sig[MAX_SIG_SIZE];
4242
__u32 sig_size;
43-
__u32 user_keyring_serial;
43+
__s32 user_keyring_serial;
4444

4545
SEC("lsm.s/file_open")
4646
int BPF_PROG(test_file_open, struct file *f)

tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define MAX_SIG_SIZE 1024
1818

1919
__u32 monitored_pid;
20-
__u32 user_keyring_serial;
20+
__s32 user_keyring_serial;
2121
__u64 system_keyring_id;
2222

2323
struct data {

tools/testing/selftests/bpf/progs/verifier_ref_tracking.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct bpf_key {} __attribute__((preserve_access_index));
2727

2828
extern void bpf_key_put(struct bpf_key *key) __ksym;
2929
extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
30-
extern struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym;
30+
extern struct bpf_key *bpf_lookup_user_key(__s32 serial, __u64 flags) __ksym;
3131

3232
/* BTF FUNC records are not generated for kfuncs referenced
3333
* from inline assembly. These records are necessary for

0 commit comments

Comments
 (0)