Skip to content

Commit 7b15523

Browse files
FlorentRevestAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add a bpf_snprintf helper
The implementation takes inspiration from the existing bpf_trace_printk helper but there are a few differences: To allow for a large number of format-specifiers, parameters are provided in an array, like in bpf_seq_printf. Because the output string takes two arguments and the array of parameters also takes two arguments, the format string needs to fit in one argument. Thankfully, ARG_PTR_TO_CONST_STR is guaranteed to point to a zero-terminated read-only map so we don't need a format string length arg. Because the format-string is known at verification time, we also do a first pass of format string validation in the verifier logic. This makes debugging easier. Signed-off-by: Florent Revest <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent fff13c4 commit 7b15523

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
19531953
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
19541954
extern const struct bpf_func_proto bpf_copy_from_user_proto;
19551955
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
1956+
extern const struct bpf_func_proto bpf_snprintf_proto;
19561957
extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
19571958
extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
19581959
extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;

include/uapi/linux/bpf.h

+28
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,33 @@ union bpf_attr {
47084708
* Return
47094709
* The number of traversed map elements for success, **-EINVAL** for
47104710
* invalid **flags**.
4711+
*
4712+
* long bpf_snprintf(char *str, u32 str_size, const char *fmt, u64 *data, u32 data_len)
4713+
* Description
4714+
* Outputs a string into the **str** buffer of size **str_size**
4715+
* based on a format string stored in a read-only map pointed by
4716+
* **fmt**.
4717+
*
4718+
* Each format specifier in **fmt** corresponds to one u64 element
4719+
* in the **data** array. For strings and pointers where pointees
4720+
* are accessed, only the pointer values are stored in the *data*
4721+
* array. The *data_len* is the size of *data* in bytes.
4722+
*
4723+
* Formats **%s** and **%p{i,I}{4,6}** require to read kernel
4724+
* memory. Reading kernel memory may fail due to either invalid
4725+
* address or valid address but requiring a major memory fault. If
4726+
* reading kernel memory fails, the string for **%s** will be an
4727+
* empty string, and the ip address for **%p{i,I}{4,6}** will be 0.
4728+
* Not returning error to bpf program is consistent with what
4729+
* **bpf_trace_printk**\ () does for now.
4730+
*
4731+
* Return
4732+
* The strictly positive length of the formatted string, including
4733+
* the trailing zero character. If the return value is greater than
4734+
* **str_size**, **str** contains a truncated string, guaranteed to
4735+
* be zero-terminated except when **str_size** is 0.
4736+
*
4737+
* Or **-EBUSY** if the per-CPU memory copy buffer is busy.
47114738
*/
47124739
#define __BPF_FUNC_MAPPER(FN) \
47134740
FN(unspec), \
@@ -4875,6 +4902,7 @@ union bpf_attr {
48754902
FN(sock_from_file), \
48764903
FN(check_mtu), \
48774904
FN(for_each_map_elem), \
4905+
FN(snprintf), \
48784906
/* */
48794907

48804908
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/helpers.c

+50
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,54 @@ int bpf_printf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
925925
return err;
926926
}
927927

928+
#define MAX_SNPRINTF_VARARGS 12
929+
930+
BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt,
931+
const void *, data, u32, data_len)
932+
{
933+
enum bpf_printf_mod_type mod[MAX_SNPRINTF_VARARGS];
934+
u64 args[MAX_SNPRINTF_VARARGS];
935+
int err, num_args;
936+
937+
if (data_len % 8 || data_len > MAX_SNPRINTF_VARARGS * 8 ||
938+
(data_len && !data))
939+
return -EINVAL;
940+
num_args = data_len / 8;
941+
942+
/* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
943+
* can safely give an unbounded size.
944+
*/
945+
err = bpf_printf_prepare(fmt, UINT_MAX, data, args, mod, num_args);
946+
if (err < 0)
947+
return err;
948+
949+
/* Maximumly we can have MAX_SNPRINTF_VARARGS parameters, just give
950+
* all of them to snprintf().
951+
*/
952+
err = snprintf(str, str_size, fmt, BPF_CAST_FMT_ARG(0, args, mod),
953+
BPF_CAST_FMT_ARG(1, args, mod), BPF_CAST_FMT_ARG(2, args, mod),
954+
BPF_CAST_FMT_ARG(3, args, mod), BPF_CAST_FMT_ARG(4, args, mod),
955+
BPF_CAST_FMT_ARG(5, args, mod), BPF_CAST_FMT_ARG(6, args, mod),
956+
BPF_CAST_FMT_ARG(7, args, mod), BPF_CAST_FMT_ARG(8, args, mod),
957+
BPF_CAST_FMT_ARG(9, args, mod), BPF_CAST_FMT_ARG(10, args, mod),
958+
BPF_CAST_FMT_ARG(11, args, mod));
959+
960+
bpf_printf_cleanup();
961+
962+
return err + 1;
963+
}
964+
965+
const struct bpf_func_proto bpf_snprintf_proto = {
966+
.func = bpf_snprintf,
967+
.gpl_only = true,
968+
.ret_type = RET_INTEGER,
969+
.arg1_type = ARG_PTR_TO_MEM_OR_NULL,
970+
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
971+
.arg3_type = ARG_PTR_TO_CONST_STR,
972+
.arg4_type = ARG_PTR_TO_MEM_OR_NULL,
973+
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
974+
};
975+
928976
const struct bpf_func_proto bpf_get_current_task_proto __weak;
929977
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
930978
const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
@@ -1013,6 +1061,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
10131061
return &bpf_probe_read_kernel_str_proto;
10141062
case BPF_FUNC_snprintf_btf:
10151063
return &bpf_snprintf_btf_proto;
1064+
case BPF_FUNC_snprintf:
1065+
return &bpf_snprintf_proto;
10161066
default:
10171067
return NULL;
10181068
}

kernel/bpf/verifier.c

+41
Original file line numberDiff line numberDiff line change
@@ -5918,6 +5918,41 @@ static int check_reference_leak(struct bpf_verifier_env *env)
59185918
return state->acquired_refs ? -EINVAL : 0;
59195919
}
59205920

5921+
static int check_bpf_snprintf_call(struct bpf_verifier_env *env,
5922+
struct bpf_reg_state *regs)
5923+
{
5924+
struct bpf_reg_state *fmt_reg = &regs[BPF_REG_3];
5925+
struct bpf_reg_state *data_len_reg = &regs[BPF_REG_5];
5926+
struct bpf_map *fmt_map = fmt_reg->map_ptr;
5927+
int err, fmt_map_off, num_args;
5928+
u64 fmt_addr;
5929+
char *fmt;
5930+
5931+
/* data must be an array of u64 */
5932+
if (data_len_reg->var_off.value % 8)
5933+
return -EINVAL;
5934+
num_args = data_len_reg->var_off.value / 8;
5935+
5936+
/* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const
5937+
* and map_direct_value_addr is set.
5938+
*/
5939+
fmt_map_off = fmt_reg->off + fmt_reg->var_off.value;
5940+
err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr,
5941+
fmt_map_off);
5942+
if (err)
5943+
return err;
5944+
fmt = (char *)(long)fmt_addr + fmt_map_off;
5945+
5946+
/* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we
5947+
* can focus on validating the format specifiers.
5948+
*/
5949+
err = bpf_printf_prepare(fmt, UINT_MAX, NULL, NULL, NULL, num_args);
5950+
if (err < 0)
5951+
verbose(env, "Invalid format string\n");
5952+
5953+
return err;
5954+
}
5955+
59215956
static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
59225957
int *insn_idx_p)
59235958
{
@@ -6032,6 +6067,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
60326067
return -EINVAL;
60336068
}
60346069

6070+
if (func_id == BPF_FUNC_snprintf) {
6071+
err = check_bpf_snprintf_call(env, regs);
6072+
if (err < 0)
6073+
return err;
6074+
}
6075+
60356076
/* reset caller saved regs */
60366077
for (i = 0; i < CALLER_SAVED_REGS; i++) {
60376078
mark_reg_not_init(env, regs, caller_saved[i]);

kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10761076
return &bpf_task_storage_delete_proto;
10771077
case BPF_FUNC_for_each_map_elem:
10781078
return &bpf_for_each_map_elem_proto;
1079+
case BPF_FUNC_snprintf:
1080+
return &bpf_snprintf_proto;
10791081
default:
10801082
return NULL;
10811083
}

tools/include/uapi/linux/bpf.h

+28
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,33 @@ union bpf_attr {
47084708
* Return
47094709
* The number of traversed map elements for success, **-EINVAL** for
47104710
* invalid **flags**.
4711+
*
4712+
* long bpf_snprintf(char *str, u32 str_size, const char *fmt, u64 *data, u32 data_len)
4713+
* Description
4714+
* Outputs a string into the **str** buffer of size **str_size**
4715+
* based on a format string stored in a read-only map pointed by
4716+
* **fmt**.
4717+
*
4718+
* Each format specifier in **fmt** corresponds to one u64 element
4719+
* in the **data** array. For strings and pointers where pointees
4720+
* are accessed, only the pointer values are stored in the *data*
4721+
* array. The *data_len* is the size of *data* in bytes.
4722+
*
4723+
* Formats **%s** and **%p{i,I}{4,6}** require to read kernel
4724+
* memory. Reading kernel memory may fail due to either invalid
4725+
* address or valid address but requiring a major memory fault. If
4726+
* reading kernel memory fails, the string for **%s** will be an
4727+
* empty string, and the ip address for **%p{i,I}{4,6}** will be 0.
4728+
* Not returning error to bpf program is consistent with what
4729+
* **bpf_trace_printk**\ () does for now.
4730+
*
4731+
* Return
4732+
* The strictly positive length of the formatted string, including
4733+
* the trailing zero character. If the return value is greater than
4734+
* **str_size**, **str** contains a truncated string, guaranteed to
4735+
* be zero-terminated except when **str_size** is 0.
4736+
*
4737+
* Or **-EBUSY** if the per-CPU memory copy buffer is busy.
47114738
*/
47124739
#define __BPF_FUNC_MAPPER(FN) \
47134740
FN(unspec), \
@@ -4875,6 +4902,7 @@ union bpf_attr {
48754902
FN(sock_from_file), \
48764903
FN(check_mtu), \
48774904
FN(for_each_map_elem), \
4905+
FN(snprintf), \
48784906
/* */
48794907

48804908
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)