normal/cmdline: fix hist_lines state loss and grub calloc failure check
PATCH 1/1 normal/cmdline: fix hist_lines state loss and grub calloc failure check
Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption.
Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.
--- grub-core/normal/cmdline.c | 7 +++++++ 1 file changed, 7 insertions(+)
grub-core/normal/cmdline.c +7 -0
@@ -42,7 +42,14 @@ grub_err_t grub_set_history (int newsize) {grub_uint32_t **old_hist_lines = hist_lines; + hist_lines =grub_calloc (newsize,sizeof (grub_uint32_t *)); +if (hist_lines ==NULL ) + { +/* We need to restore hist_lines to avoid memory leak and state loss */ + hist_lines = old_hist_lines; +return grub_errno; + }/* Copy the old lines into the new buffer. */ if (old_hist_lines)
Signature · 7 lines
2.47.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 11 lines · Avnish Chouhan
Adding a fix for
hist_linesmemory leak and state loss. In current code, we overwritehist_linesbefore checking the allocation result. Ifgrub_callocfails,hist_linesbecomes NULL and we loose the reference to the previously allocatedhist_lines. With this new change. On failure,hist_linesremains pointing to the old valid memory. No leak, no state corruption.
Quoted 5 lines
Along with this, adding a failure check in
grub_calloc(). Ifgrub_callocfails, (e.g., due to memory allocation failure), it returns NULL. Then, passinghist_lines(which would be NULL) togrub_memmove()will result in a null pointer dereference, and can cause an undefined behavior.
Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.
Thanks,
Sudhakar
Quoted 33 lines
Signed-off-by Avnish Chouhan <[email protected]>---
grub-core/normal/cmdline.c| 7 +++++++1 file changed, 7 insertions(+)grub-core/normal/cmdline.c +7
-0@@ -42,7 +42,14 @@ grub_err_t grub_set_history (int newsize) {grub_uint32_t **old_hist_lines = hist_lines; + hist_lines =grub_calloc (newsize,sizeof (grub_uint32_t *)); +if (hist_lines ==NULL ) + { +/* We need to restore hist_lines to avoid memory leak and state loss */ + hist_lines = old_hist_lines; +return grub_errno; + }/* Copy the old lines into the new buffer. */ if (old_hist_lines)Signature · 7 lines
2.47.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 15 lines · Sudhakar Kuppusamy
Quoted 9 lines · Avnish Chouhan
Adding a fix for
hist_linesmemory leak and state loss. In current code, we overwritehist_linesbefore checking the allocation result. Ifgrub_callocfails,hist_linesbecomes NULL and we loose the reference to the previously allocatedhist_lines. With this new change. On failure,hist_linesremains pointing to the old valid memory. No leak, no state corruption.Quoted 5 lines
Along with this, adding a failure check in
grub_calloc(). Ifgrub_callocfails, (e.g., due to memory allocation failure), it returns NULL. Then, passinghist_lines(which would be NULL) togrub_memmove()will result in a null pointer dereference, and can cause an undefined behavior.
Quoted 5 lines
Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.
Thanks,
Sudhakar
This commit message clearly defines the problem and the intended purpose of this patch!
Quoted 30 lines
Quoted 30 lines
Signed-off-by Avnish Chouhan <[email protected]>---
grub-core/normal/cmdline.c| 7 +++++++1 file changed, 7 insertions(+)grub-core/normal/cmdline.c +4
-0@@ -42,7 +42,14 @@ grub_err_t grub_set_history (int newsize) {grub_uint32_t **old_hist_lines = hist_lines; + hist_lines =grub_calloc (newsize,sizeof (grub_uint32_t *)); +if (hist_lines ==NULL ) + { +/* We need to restore hist_lines to avoid memory leak and state loss */
grub-core/normal/cmdline.c +3
-1+ hist_lines = old_hist_lines; +return grub_errno; + }/* Copy the old lines into the new buffer. */ if (old_hist_lines) --2.47.1
Quoted 4 lines
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 7 lines · Avnish Chouhan
Adding a fix for
hist_linesmemory leak and state loss. In current code, we overwritehist_linesbefore checking the allocation result. Ifgrub_callocfails,hist_linesbecomes NULL and we loose the reference to the previously allocatedhist_lines. With this new change. On failure,hist_linesremains pointing to the old valid memory. No leak, no state corruption.
Quoted 7 lines
Along with this, adding a failure check in
grub_calloc(). Ifgrub_callocfails, (e.g., due to memory allocation failure), it returns NULL. Then, passinghist_lines(which would be NULL) togrub_memmove()will result in a null pointer dereference, and can cause an undefined behavior.Signed-off-by Avnish Chouhan <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 17 lines · Avnish Chouhan
Quoted 12 lines · Sudhakar Kuppusamy
Quoted 12 lines · Avnish Chouhan
Adding a fix for
hist_linesmemory leak and state loss. In current code, we overwritehist_linesbefore checking the allocation result. Ifgrub_callocfails,hist_linesbecomes NULL and we loose the reference to the previously allocatedhist_lines. With this new change. On failure,hist_linesremains pointing to the old valid memory. No leak, no state corruption. Along with this, adding a failure check ingrub_calloc(). Ifgrub_callocfails, (e.g., due to memory allocation failure), it returns NULL. Then, passinghist_lines(which would be NULL) togrub_memmove()will result in a null pointer dereference, and can cause an undefined behavior.
Quoted 6 lines
Quoted 4 lines
Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.
Thanks,
SudhakarThis commit message clearly defines the problem and the intended purpose of this patch!
In the first message you defines the problem and also mentioned the solution. The second message is confusing to me.
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Signed-off-by Avnish Chouhan <[email protected]>---
grub-core/normal/cmdline.c| 7 +++++++1 file changed, 7 insertions(+)grub-core/normal/cmdline.c +7
-0@@ -42,7 +42,14 @@ grub_err_t grub_set_history (int newsize) {grub_uint32_t **old_hist_lines = hist_lines; + hist_lines =grub_calloc (newsize,sizeof (grub_uint32_t *)); +if (hist_lines ==NULL ) + { +/* We need to restore hist_lines to avoid memory leak and state loss */ + hist_lines = old_hist_lines; +return grub_errno; + }
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
grub-core/normal/cmdline.c +0
-1/* Copy the old lines into the new buffer. */ if (old_hist_lines) --2.47.1
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel