samples: bpf: Fix a couple of warnings
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Sat, 21 Aug 2021 00:19:49 +0000 (05:49 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 24 Aug 2021 21:48:40 +0000 (14:48 -0700)
cookie_uid_helper_example.c: In function ‘main’:
cookie_uid_helper_example.c:178:69: warning: ‘ -j ACCEPT’ directive
writing 10 bytes into a region of size between 8 and 58
[-Wformat-overflow=]
  178 |  sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
      |        ^~~~~~~~~~
/home/kkd/src/linux/samples/bpf/cookie_uid_helper_example.c:178:9: note:
‘sprintf’ output between 53 and 103 bytes into a destination of size 100
  178 |  sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179 |         file);
      |         ~~~~~

Fix by using snprintf and a sufficiently sized buffer.

tracex4_user.c:35:15: warning: ‘write’ reading 12 bytes from a region of
size 11 [-Wstringop-overread]
   35 |         key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use size as 11.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210821002010.845777-2-memxor@gmail.com
samples/bpf/cookie_uid_helper_example.c
samples/bpf/tracex4_user.c

index cc3bce8d3aac5db207af3f2b22ce307335b58534..54958802c032633560cada425070eda6b12356af 100644 (file)
@@ -167,7 +167,7 @@ static void prog_load(void)
 static void prog_attach_iptables(char *file)
 {
        int ret;
-       char rules[100];
+       char rules[256];
 
        if (bpf_obj_pin(prog_fd, file))
                error(1, errno, "bpf_obj_pin");
@@ -175,8 +175,13 @@ static void prog_attach_iptables(char *file)
                printf("file path too long: %s\n", file);
                exit(1);
        }
-       sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
-               file);
+       ret = snprintf(rules, sizeof(rules),
+                      "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
+                      file);
+       if (ret < 0 || ret >= sizeof(rules)) {
+               printf("error constructing iptables command\n");
+               exit(1);
+       }
        ret = system(rules);
        if (ret < 0) {
                printf("iptables rule update failed: %d/n", WEXITSTATUS(ret));
index cea399424bca9ee020605429f814d59a9c2bf7b8..566e6440e8c2fb1af02a7d4cb1a796896931ee7d 100644 (file)
@@ -32,7 +32,7 @@ static void print_old_objects(int fd)
        __u64 key, next_key;
        struct pair v;
 
-       key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
+       key = write(1, "\e[1;1H\e[2J", 11); /* clear screen */
 
        key = -1;
        while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {