|
| 1 | +From 52a9dab6d892763b2a8334a568bd4e2c1a6fde66 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kees Cook < [email protected]> |
| 3 | +Date: Sun, 13 Feb 2022 10:24:43 -0800 |
| 4 | +Subject: [PATCH] libsubcmd: Fix use-after-free for realloc(..., 0) |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=UTF-8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | + |
| 9 | +GCC 12 correctly reports a potential use-after-free condition in the |
| 10 | +xrealloc helper. Fix the warning by avoiding an implicit "free(ptr)" |
| 11 | +when size == 0: |
| 12 | + |
| 13 | +In file included from help.c:12: |
| 14 | +In function 'xrealloc', |
| 15 | + inlined from 'add_cmdname' at help.c:24:2: subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free] |
| 16 | + 56 | ret = realloc(ptr, size); |
| 17 | + | ^~~~~~~~~~~~~~~~~~ |
| 18 | +subcmd-util.h:52:21: note: call to 'realloc' here |
| 19 | + 52 | void *ret = realloc(ptr, size); |
| 20 | + | ^~~~~~~~~~~~~~~~~~ |
| 21 | +subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free] |
| 22 | + 58 | ret = realloc(ptr, 1); |
| 23 | + | ^~~~~~~~~~~~~~~ |
| 24 | +subcmd-util.h:52:21: note: call to 'realloc' here |
| 25 | + 52 | void *ret = realloc(ptr, size); |
| 26 | + | ^~~~~~~~~~~~~~~~~~ |
| 27 | + |
| 28 | +Fixes: 2f4ce5ec1d447beb ("perf tools: Finalize subcmd independence") |
| 29 | +Reported-by: Valdis Klētnieks < [email protected]> |
| 30 | +Signed-off-by: Kees Kook < [email protected]> |
| 31 | +Tested-by: Valdis Klētnieks < [email protected]> |
| 32 | +Tested-by: Justin M. Forbes < [email protected]> |
| 33 | +Acked-by: Josh Poimboeuf < [email protected]> |
| 34 | + |
| 35 | +Cc: Valdis Klētnieks < [email protected]> |
| 36 | +Link: http://lore.kernel.org/lkml/ [email protected] |
| 37 | +Signed-off-by: Arnaldo Carvalho de Melo < [email protected]> |
| 38 | +--- |
| 39 | + tools/lib/subcmd/subcmd-util.h | 11 ++--------- |
| 40 | + 1 file changed, 2 insertions(+), 9 deletions(-) |
| 41 | + |
| 42 | +diff --git a/tools/lib/subcmd/subcmd-util.h b/tools/lib/subcmd/subcmd-util.h |
| 43 | +index 794a375dad3601..b2aec04fce8f67 100644 |
| 44 | +--- a/tools/lib/subcmd/subcmd-util.h |
| 45 | ++++ b/tools/lib/subcmd/subcmd-util.h |
| 46 | +@@ -50,15 +50,8 @@ static NORETURN inline void die(const char *err, ...) |
| 47 | + static inline void *xrealloc(void *ptr, size_t size) |
| 48 | + { |
| 49 | + void *ret = realloc(ptr, size); |
| 50 | +- if (!ret && !size) |
| 51 | +- ret = realloc(ptr, 1); |
| 52 | +- if (!ret) { |
| 53 | +- ret = realloc(ptr, size); |
| 54 | +- if (!ret && !size) |
| 55 | +- ret = realloc(ptr, 1); |
| 56 | +- if (!ret) |
| 57 | +- die("Out of memory, realloc failed"); |
| 58 | +- } |
| 59 | ++ if (!ret) |
| 60 | ++ die("Out of memory, realloc failed"); |
| 61 | + return ret; |
| 62 | + } |
| 63 | + |
| 64 | + |
0 commit comments