Debian Codegraph @

vmtest/patches/libsubcmd-Fix-use-after-free-for-realloc-.-0.patch

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