Skip to content

Commit ef66c54

Browse files
Byte-LabAlexei Starovoitov
authored andcommitted
bpf: Allow multiple modifiers in reg_type_str() prefix
reg_type_str() in the verifier currently only allows a single register type modifier to be present in the 'prefix' string which is eventually stored in the env type_str_buf. This currently works fine because there are no overlapping type modifiers, but once PTR_TRUSTED is added, that will no longer be the case. This patch updates reg_type_str() to support having multiple modifiers in the prefix string, and updates the size of type_str_buf to be 128 bytes. Signed-off-by: David Vernet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ee748cd commit ef66c54

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
#define BPF_MAX_VAR_SIZ (1 << 29)
2121
/* size of type_str_buf in bpf_verifier. */
22-
#define TYPE_STR_BUF_LEN 64
22+
#define TYPE_STR_BUF_LEN 128
2323

2424
/* Liveness marks, used for registers and spilled-regs (in stack slots).
2525
* Read marks propagate upwards until they find a write mark; they record that

kernel/bpf/verifier.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn)
557557
static const char *reg_type_str(struct bpf_verifier_env *env,
558558
enum bpf_reg_type type)
559559
{
560-
char postfix[16] = {0}, prefix[32] = {0};
560+
char postfix[16] = {0}, prefix[64] = {0};
561561
static const char * const str[] = {
562562
[NOT_INIT] = "?",
563563
[SCALAR_VALUE] = "scalar",
@@ -589,16 +589,13 @@ static const char *reg_type_str(struct bpf_verifier_env *env,
589589
strncpy(postfix, "_or_null", 16);
590590
}
591591

592-
if (type & MEM_RDONLY)
593-
strncpy(prefix, "rdonly_", 32);
594-
if (type & MEM_RINGBUF)
595-
strncpy(prefix, "ringbuf_", 32);
596-
if (type & MEM_USER)
597-
strncpy(prefix, "user_", 32);
598-
if (type & MEM_PERCPU)
599-
strncpy(prefix, "percpu_", 32);
600-
if (type & PTR_UNTRUSTED)
601-
strncpy(prefix, "untrusted_", 32);
592+
snprintf(prefix, sizeof(prefix), "%s%s%s%s%s",
593+
type & MEM_RDONLY ? "rdonly_" : "",
594+
type & MEM_RINGBUF ? "ringbuf_" : "",
595+
type & MEM_USER ? "user_" : "",
596+
type & MEM_PERCPU ? "percpu_" : "",
597+
type & PTR_UNTRUSTED ? "untrusted_" : ""
598+
);
602599

603600
snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s",
604601
prefix, str[base_type(type)], postfix);

0 commit comments

Comments
 (0)