BPF List
 help / color / mirror / Atom feed
From: "Daniel Xu" <dxu@dxuuu.xyz>
To: <bpf@vger.kernel.org>
Cc: <ast@kernel.org>
Subject: bpf_probe_read*str() may store junk after NUL terminator
Date: Tue, 03 Nov 2020 09:45:41 -0800	[thread overview]
Message-ID: <C6TTDYNLD7UX.P2O6PJF7OC39@maharaja> (raw)

Hi,

I recently received a bpftrace bug report [0] that identical strings
were being stored as separate entries in maps. I dug into the issue and
it turns out that bpf_probe_read*str() may store junk after the NUL
terminator due to how do_strncpy_from_user() does long-sized copies.

Here is the code in question from lib/strncpy_from_user.c:

       *(unsigned long *)(dst+res) = c;
       if (has_zero(c, &data, &constants)) {
               data = prep_zero_mask(c, data, &constants);
               data = create_zero_mask(data);
               return res + find_zero(data);
       }

This behavior is likely to cause subtle issues in bpf programs so a
kernel fix may be necessary.

Here is a quick reproducer:

str_trailing_bytes.c:

    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>

    const char s[] = "mestring";

    __attribute__((noinline)) void function1(char *first __attribute__((unused)),
					     char *second __attribute__((unused)))
    {
    }

    int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
    {
      char *first = malloc(64 * sizeof(char));
      char *second = malloc(64 * sizeof(char));

      // Make sure bytes after the first string are 0s
      memset(first, 0, 64 * sizeof(char));
      memcpy(first, s, sizeof(s));

      // Make sure bytes after second string are 1s
      memset(second, 1, 64 * sizeof(char));
      memcpy(second, s, sizeof(s));

      function1(first, second);

      free(first);
      free(second);
      return 0;
    }


# bpftrace -e \
  'uprobe:./str_trailing_bytes:function1 { @[str(arg0)] = count(); @[str(arg1)] = count(); exit() }' \ 
  -c ./str_trailing_bytes
Attaching 1 probe...


@[mestring]: 1
@[mestring]: 1


Thanks,
Daniel


[0]: https://github.com/iovisor/bpftrace/pull/1586/

             reply	other threads:[~2020-11-03 18:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-03 17:45 Daniel Xu [this message]
2020-11-03 18:24 ` bpf_probe_read*str() may store junk after NUL terminator Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=C6TTDYNLD7UX.P2O6PJF7OC39@maharaja \
    --to=dxu@dxuuu.xyz \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox