git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] automatically ban strcpy()

From
Jeff King <peff@peff.net>
Date
Jul 24, 2018, 09:26 UTC
Message-ID
<20180724092618.GA3288@sigill.intra.peff.net>
In-Reply-To
<20180724092329.GA24250@sigill.intra.peff.net>

There are a few standard C functions (like strcpy) which are easy to misuse. E.g.:

  char path[PATH_MAX];
  strcpy(path, arg);

may overflow the "path" buffer. Sometimes there's an earlier constraint on the size of "arg", but even in such a case it's hard to verify that the code is correct. If the size really is unbounded, you're better off using a dynamic helper like strbuf:

  struct strbuf path = STRBUF_INIT;
  strbuf_addstr(path, arg);

or if it really is bounded, then use xsnprintf to show your expectation (and get a run-time assertion):

  char path[PATH_MAX];
  xsnprintf(path, sizeof(path), "%s", arg);
which makes further auditing easier.

We'd usually catch undesirable code like this in a review, but there's no automated enforcement. Adding that enforcement can help us be more consistent and save effort (and a round-trip) during review.

This patch teaches the compiler to report an error when it sees strcpy (and will become a model for banning a few other functions). This has a few advantages over a separate linting tool:

  1. We know it's run as part of a build cycle, so it's
     hard to ignore. Whereas an external linter is an extra
     step the developer needs to remember to do.
  2. Likewise, it's basically free since the compiler is
     parsing the code anyway.
  3. We know it's robust against false positives (unlike a
     grep-based linter).
The two big disadvantages are:
  1. We'll only check code that is actually compiled, so it
     may miss code that isn't triggered on your particular
     system. But since presumably people don't add new code
     without compiling it (and if they do, the banned
     function list is the least of their worries), we really
     only care about failing to clean up old code when
     adding new functions to the list. And that's easy
     enough to address with a manual audit when adding a new
     function.
  2. If this ends up generating false positives, it's going
     to be harder to disable (as opposed to a separate
     linter, which may have mechanisms for overriding a
     particular case).
     But the intent is to only ban functions which are
     obviously bad, and for which we accept using an
     alternative even when this particular use isn't buggy
     (e.g., the xsnprintf alternative above).

The implementation here is simple: we'll define a macro for the banned function which replaces it with a descriptively named but undefined function. Replacing it with any invalid code would work (since we just want to break compilation). But ideally we'd meet these goals:

 - it should be portable; ideally this would trigger
   everywhere, and does not need to be part of a DEVELOPER=1
   setup (because unlike warnings which may depend on the
   compiler or system, this is a clear indicator of
   something wrong in the code).
 - it should generate a readable error that gives the
   developer a clue what happened
 - it should avoid generating too much other cruft that
   makes it hard to see the actual error
 - it should mention the original callsite in the error

The output with this patch looks like this (using gcc 7, on a checkout without 022d2ac1f3, which removes the final strcpy from blame.c):

      CC builtin/blame.o
  In file included from ./git-compat-util.h:1242:0,
                   from ./cache.h:4,
                   from builtin/blame.c:8:
  builtin/blame.c: In function ‘cmd_blame’:
  ./banned.h:11:22: error: implicit declaration of function ‘sorry_strcpy_is_a_banned_function’ [-Werror=implicit-function-declaration]
   #define BANNED(func) sorry_##func##_is_a_banned_function()
                        ^
  ./banned.h:13:21: note: in expansion of macro ‘BANNED’
   #define strcpy(x,y) BANNED(strcpy)
                       ^~~~~~
  builtin/blame.c:1074:4: note: in expansion of macro ‘strcpy’
      strcpy(repeated_meta_color, GIT_COLOR_CYAN);
      ^~~~~~

This pretty clearly shows the original callsite along with the descriptive function name, and it mentions banned.h, which contains more information.

What's not perfectly ideal is:
  1. We'll only trigger with -Wimplicit-function-declaration
     (and only stop compilation with -Werror). These are
     generally enabled by DEVELOPER=1. If you _don't_ have
     that set, we'll still catch the problem, but only at
     link-time, with a slightly less useful message:
       /usr/bin/ld: builtin/blame.o: in function `cmd_blame':
       /home/peff/tmp/builtin/blame.c:1074: undefined reference to `sorry_strcpy_is_a_banned_function'
       collect2: error: ld returned 1 exit status
     If instead we convert this to a reference to an
     undefined variable, that always dies immediately. But
     gcc seems to print the set of errors twice, which
     clutters things up.
  2. The expansion through BANNED() adds an extra layer of
     error. Curiously, though, removing it (and just
     expanding strcpy directly to the bogus function name)
     causes gcc _not_ to report the original line of code.

So experimentally, this seems to behave pretty well on gcc. Clang looks OK, too, though:

 - it actually produces a better message for the undeclared
   identifier than for the implicit function (it doesn't
   double the errors, and for the implicit function it
   actually produces an extra complaint about
   strict-prototypes).
 - the BANNED indirection has no effect (it shows the
   original line of code either way)

So if we want to optimize for clang's output, we could switch both of those decisions.

Note that the linux kernel has a similar mechanism which goes by BUILD_BUG_ON_MSG(). It works by declaring a one-off function with gcc's error attribute. That doesn't work for us here, because we'd like to work even on non-gcc compilers (and in my opinion the compiler output is actually _less_ readable).

Signed-off-by: Jeff King <peff@peff.net>
---
 banned.h          | 16 ++++++++++++++++
 git-compat-util.h |  6 ++++++
 2 files changed, 22 insertions(+)
 create mode 100644 banned.h
diff --git a/banned.h b/banned.h
new file mode 100644
index 0000000000..c50091ad7d
--- /dev/null
+++ b/banned.h
@@ -0,0 +1,16 @@
+#ifndef BANNED_H
+#define BANNED_H
+
+/*
+ * This header lists functions that have been banned from our code base,
+ * because they're too easy to misuse (and even if used correctly,
+ * complicate audits). Including this header turns them into compile-time
+ * errors.
+ */
+
+#define BANNED(func) sorry_##func##_is_a_banned_function()
+
+#undef strcpy
+#define strcpy(x,y) BANNED(strcpy)
+
+#endif /* BANNED_H */
diff --git a/git-compat-util.h b/git-compat-util.h
index 9a64998b24..89d37095c7 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1239,4 +1239,10 @@ extern void unleak_memory(const void *ptr, size_t len);
 #define UNLEAK(var) do {} while (0)
 #endif
 
+/*
+ * This include must come after system headers, since it introduces macros that
+ * replace system names.
+ */
+#include "banned.h"
+
 #endif
-- 
2.18.0.542.g2bf2fc4f7e
Previous: Jeff KingNext: Eric Sunshine
Message 32 of 42 in “fail compilation with strcpy”
  1. 0/2 fail compilation with strcpyJeff King, Jul 19, 2018
  2. 1/2 introduce "banned function" listJeff King, Jul 19, 2018
  3. Eric SunshineJul 19, 2018
  4. Jeff KingJul 19, 2018
  5. Eric SunshineJul 19, 2018
  6. Jeff KingJul 20, 2018
  7. Stefan BellerJul 19, 2018
  8. Jeff KingJul 19, 2018
  9. Stefan BellerJul 19, 2018
  10. Jeff KingJul 20, 2018
  11. Junio C HamanoJul 19, 2018
  12. Randall S. BeckerJul 19, 2018
  13. Jeff KingJul 20, 2018
  14. Jeff KingJul 20, 2018
  15. Junio C HamanoJul 20, 2018
  16. Jeff KingJul 20, 2018
  17. Theodore Y. Ts'oJul 20, 2018
  18. Jeff KingJul 20, 2018
  19. Junio C HamanoJul 20, 2018
  20. Derrick StoleeJul 20, 2018
  21. Duy NguyenJul 20, 2018
  22. Elijah NewrenJul 20, 2018
  23. Jeff KingJul 20, 2018
  24. Jeff KingJul 20, 2018
  25. 2/2 banned.h: mark strncpy as bannedJeff King, Jul 19, 2018
  26. Ævar Arnfjörð BjarmasonJul 19, 2018
  27. Jeff KingJul 19, 2018
  28. Junio C HamanoJul 20, 2018
  29. Jeff KingJul 20, 2018
  30. Junio C HamanoJul 20, 2018
  31. 0/4 fail compilation with strcpyJeff King, Jul 24, 2018
  32. 1/4 automatically ban strcpy()Jeff King, Jul 24, 2018
  33. Eric SunshineJul 24, 2018
  34. Jeff KingJul 26, 2018
  35. 1/4 automatically ban strcpy()Jeff King, Jul 26, 2018
  36. Junio C HamanoJul 26, 2018
  37. Jeff KingJul 27, 2018
  38. Junio C HamanoJul 27, 2018
  39. Jeff KingJul 28, 2018
  40. 2/4 banned.h: mark strcat() as bannedJeff King, Jul 24, 2018
  41. 3/4 banned.h: mark sprintf() as bannedJeff King, Jul 24, 2018
  42. 4/4 banned.h: mark strncpy() as bannedJeff King, Jul 24, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.