Skip to content

Commit af7d5be

Browse files
liu-song-6Kernel Patches Daemon
authored andcommitted
bpf: Allow const char * from LSM hooks as kfunc const string arguments
Let the BPF verifier to recognize const char * arguments from LSM hooks (and other BPF program types) as valid const string pointers that can be passed to kfuncs expecting KF_ARG_PTR_TO_CONST_STR. Previously, kfuncs with KF_ARG_PTR_TO_CONST_STR only accepted PTR_TO_MAP_VALUE from readonly maps. This was limiting for LSM programs that receive const char * arguments from hooks like sb_mount's dev_name. Signed-off-by: Song Liu <[email protected]>
1 parent 8c83cb5 commit af7d5be

File tree

3 files changed

+73
-12
lines changed

3 files changed

+73
-12
lines changed

include/linux/btf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct btf *btf_base_btf(const struct btf *btf);
224224
bool btf_type_is_i32(const struct btf_type *t);
225225
bool btf_type_is_i64(const struct btf_type *t);
226226
bool btf_type_is_primitive(const struct btf_type *t);
227+
bool btf_type_is_const_char_ptr(const struct btf *btf, const struct btf_type *t);
227228
bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
228229
const struct btf_member *m,
229230
u32 expected_offset, u32 expected_size);

kernel/bpf/btf.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,25 @@ bool btf_type_is_primitive(const struct btf_type *t)
897897
btf_is_any_enum(t);
898898
}
899899

900+
bool btf_type_is_const_char_ptr(const struct btf *btf, const struct btf_type *t)
901+
{
902+
const char *tname;
903+
904+
/* The type chain has to be PTR->CONST->CHAR */
905+
if (BTF_INFO_KIND(t->info) != BTF_KIND_PTR)
906+
return false;
907+
908+
t = btf_type_by_id(btf, t->type);
909+
if (BTF_INFO_KIND(t->info) != BTF_KIND_CONST)
910+
return false;
911+
912+
t = btf_type_by_id(btf, t->type);
913+
tname = btf_name_by_offset(btf, t->name_off);
914+
if (tname && strcmp(tname, "char") == 0)
915+
return true;
916+
return false;
917+
}
918+
900919
/*
901920
* Check that given struct member is a regular int with expected
902921
* offset and size.
@@ -6746,6 +6765,20 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
67466765
/* Default prog with MAX_BPF_FUNC_REG_ARGS args */
67476766
return true;
67486767
t = btf_type_by_id(btf, args[arg].type);
6768+
6769+
/*
6770+
* For const string, we need to match "const char *"
6771+
* exactly. Therefore, do the check before the skipping
6772+
* modifiers.
6773+
*/
6774+
if (btf_type_is_const_char_ptr(btf, t)) {
6775+
info->reg_type = PTR_TO_BTF_ID;
6776+
if (prog_args_trusted(prog))
6777+
info->reg_type |= PTR_TRUSTED;
6778+
info->btf = btf;
6779+
info->btf_id = args[arg].type;
6780+
return true;
6781+
}
67496782
}
67506783

67516784
/* skip modifiers */

kernel/bpf/verifier.c

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9598,18 +9598,19 @@ static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,
95989598
return state->stack[spi].spilled_ptr.dynptr.type;
95999599
}
96009600

9601-
static int check_reg_const_str(struct bpf_verifier_env *env,
9602-
struct bpf_reg_state *reg, u32 regno)
9601+
/*
9602+
* Check for const string saved in a bpf map. The caller is responsible
9603+
* to check reg->type == PTR_TO_MAP_VALUE.
9604+
*/
9605+
static int check_reg_const_str_in_map(struct bpf_verifier_env *env,
9606+
struct bpf_reg_state *reg, u32 regno)
96039607
{
96049608
struct bpf_map *map = reg->map_ptr;
96059609
int err;
96069610
int map_off;
96079611
u64 map_addr;
96089612
char *str_ptr;
96099613

9610-
if (reg->type != PTR_TO_MAP_VALUE)
9611-
return -EINVAL;
9612-
96139614
if (!bpf_map_is_rdonly(map)) {
96149615
verbose(env, "R%d does not point to a readonly map'\n", regno);
96159616
return -EACCES;
@@ -9646,6 +9647,26 @@ static int check_reg_const_str(struct bpf_verifier_env *env,
96469647
return 0;
96479648
}
96489649

9650+
/* Check for const string passed in as input to the bpf program. */
9651+
static int check_reg_const_str_arg(struct bpf_reg_state *reg)
9652+
{
9653+
const struct btf *btf;
9654+
const struct btf_type *t;
9655+
const char *tname;
9656+
9657+
if (base_type(reg->type) != PTR_TO_BTF_ID)
9658+
return -EINVAL;
9659+
9660+
btf = reg->btf;
9661+
t = btf_type_by_id(btf, reg->btf_id);
9662+
if (!t)
9663+
return -EINVAL;
9664+
9665+
if (btf_type_is_const_char_ptr(btf, t))
9666+
return 0;
9667+
return -EINVAL;
9668+
}
9669+
96499670
/* Returns constant key value in `value` if possible, else negative error */
96509671
static int get_constant_map_key(struct bpf_verifier_env *env,
96519672
struct bpf_reg_state *key,
@@ -9964,7 +9985,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
99649985
break;
99659986
case ARG_PTR_TO_CONST_STR:
99669987
{
9967-
err = check_reg_const_str(env, reg, regno);
9988+
if (reg->type != PTR_TO_MAP_VALUE)
9989+
return -EINVAL;
9990+
err = check_reg_const_str_in_map(env, reg, regno);
99689991
if (err)
99699992
return err;
99709993
break;
@@ -13626,13 +13649,17 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1362613649
meta->arg_btf_id = reg->btf_id;
1362713650
break;
1362813651
case KF_ARG_PTR_TO_CONST_STR:
13629-
if (reg->type != PTR_TO_MAP_VALUE) {
13630-
verbose(env, "arg#%d doesn't point to a const string\n", i);
13631-
return -EINVAL;
13652+
if (reg->type == PTR_TO_MAP_VALUE) {
13653+
ret = check_reg_const_str_in_map(env, reg, regno);
13654+
if (ret)
13655+
return ret;
13656+
} else {
13657+
ret = check_reg_const_str_arg(reg);
13658+
if (ret) {
13659+
verbose(env, "arg#%d doesn't point to a const string\n", i);
13660+
return ret;
13661+
}
1363213662
}
13633-
ret = check_reg_const_str(env, reg, regno);
13634-
if (ret)
13635-
return ret;
1363613663
break;
1363713664
case KF_ARG_PTR_TO_WORKQUEUE:
1363813665
if (reg->type != PTR_TO_MAP_VALUE) {

0 commit comments

Comments
 (0)