From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1751) id 42AB5384F4B1; Mon, 21 Nov 2022 21:02:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42AB5384F4B1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669064569; bh=dt/GV23kov26tYykgUGht9sSNAXPuHyHsIddlD6eriw=; h=From:To:Subject:Date:From; b=hS345xrzml+8RFyjksNKnrBW3AGh9WlB/c3PII4SU9L946TsL59TRedoVO9JgvIId xoGty/YcHw/pbvLwGBK/3GylqKdM8Rjv4VIeHZrRNv3tDf8XRJBlJihihqQnvC3ijF +pJ9Y1hDPHbPkU6wIZQ0wGCNMvYKyBerse8B0urk= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Philippe Waroquiers To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix use after free introduced by $_hit_bpnum/$_hit_locno variables. X-Act-Checkin: binutils-gdb X-Git-Author: Philippe Waroquiers X-Git-Refname: refs/heads/master X-Git-Oldrev: e66c9edea34ce13e7a6769996e1d151505d8cd3e X-Git-Newrev: 83f350830ee561ddc799d1445c6024a84df9f161 Message-Id: <20221121210249.42AB5384F4B1@sourceware.org> Date: Mon, 21 Nov 2022 21:02:49 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D83f350830ee5= 61ddc799d1445c6024a84df9f161 commit 83f350830ee561ddc799d1445c6024a84df9f161 Author: Philippe Waroquiers Date: Sun Nov 20 11:11:27 2022 +0100 Fix use after free introduced by $_hit_bpnum/$_hit_locno variables. =20 If the commands of the bpstat bs contain commands such as step or next = or continue, the BS and its commands are freed by execute_control_command. =20 So, we cannot remember the BS that was printed. Instead, remember the bpnum and locno. =20 Regtested on debian/amd64 and re-run a few tests under valgrind. Diff: --- gdb/breakpoint.c | 62 ++++++++++++++++++++++++++++++++--------------------= ---- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 7f6400db624..5b691673a0e 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -4574,18 +4574,17 @@ command_line_is_silent (struct command_line *cmd) return cmd && (strcmp ("silent", cmd->line) =3D=3D 0); } =20 -/* Sets the $_hit_bpnum and $_hit_locno to the bpnum and locno of bs. */ +/* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno. + A locno 0 is changed to 1 to e.g. let the user do + (gdb) disable $_hit_bpnum.$_hit_locno + for a single location breakpoint. */ + static void -set_hit_convenience_vars (bpstat *bs) +set_hit_convenience_vars (int bpnum, int locno) { - const struct breakpoint *b =3D bs->breakpoint_at; - if (b !=3D nullptr) - { - int locno =3D bpstat_locno (bs); - set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), b->numbe= r); - set_internalvar_integer (lookup_internalvar ("_hit_locno"), - (locno > 0 ? locno : 1)); - } + set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum); + set_internalvar_integer (lookup_internalvar ("_hit_locno"), + (locno > 0 ? locno : 1)); } =20 /* Execute all the commands associated with all the breakpoints at @@ -4602,7 +4601,6 @@ bpstat_do_actions_1 (bpstat **bsp) { bpstat *bs; bool again =3D false; - bpstat *bs_print_hit_var; =20 /* Avoid endless recursion if a `source' command is contained in bs->commands. */ @@ -4618,29 +4616,39 @@ bpstat_do_actions_1 (bpstat **bsp) bs =3D *bsp; =20 /* The $_hit_* convenience variables are set before running the - commands of bs. In case we have several bs, after the loop, - we set again the variables to the first bs to print. */ - bs_print_hit_var =3D nullptr; + commands of BS. In case we have several bs, after the loop, + we set again the variables to the first printed bpnum and locno. + For multiple breakpoints, this ensures the variables are set to the + breakpoint printed for the user. */ + int printed_hit_bpnum =3D -1; + int printed_hit_locno =3D -1; =20 breakpoint_proceeded =3D 0; for (; bs !=3D NULL; bs =3D bs->next) { struct command_line *cmd =3D NULL; =20 - /* Set the _hit_* convenience variables before running the commands = of - each bs. If this is the first bs to be printed, remember it so as to - set the convenience variable again to this bs after the loop so that in - case of multiple breakpoints, the variables are set to the breakpoint - printed for the user. */ - set_hit_convenience_vars (bs); - if (bs_print_hit_var =3D=3D nullptr && bs->print) - bs_print_hit_var =3D bs; + /* Set the _hit_* convenience variables before running BS's commands= . */ + { + const struct breakpoint *b =3D bs->breakpoint_at; + if (b !=3D nullptr) + { + int locno =3D bpstat_locno (bs); + + set_hit_convenience_vars (b->number, locno); + if (printed_hit_locno =3D=3D -1 && bs->print) + { + printed_hit_bpnum =3D b->number; + printed_hit_locno =3D locno; + } + } + } =20 /* Take ownership of the BSP's command tree, if it has one. =20 The command tree could legitimately contain commands like 'step' and 'next', which call clear_proceed_status, which - frees stop_bpstat's command tree. To make sure this doesn't + frees the bpstat BS and its command tree. To make sure this doesn't free the tree we're executing out from under us, we need to take ownership of the tree ourselves. Since a given bpstat's commands are only executed once, we don't need to copy it; we @@ -4659,6 +4667,8 @@ bpstat_do_actions_1 (bpstat **bsp) while (cmd !=3D NULL) { execute_control_command (cmd); + /* After execute_control_command, if breakpoint_proceeded is true, + BS has been freed and cannot be accessed anymore. */ =20 if (breakpoint_proceeded) break; @@ -4693,9 +4703,9 @@ bpstat_do_actions_1 (bpstat **bsp) } =20 /* Now that we have executed the commands of all bs, set the _hit_* - convenience variables to the printed bs. */ - if (bs_print_hit_var !=3D nullptr) - set_hit_convenience_vars (bs_print_hit_var); + convenience variables to the printed values. */ + if (printed_hit_locno !=3D -1) + set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno); =20 return again; }