Fix using freed memory for long option arguments
PATCH 1/1 Fix using freed memory for long option arguments
ChangeLog:
* lib/arg.c (find_long_option): Remove. (find_long): Add len' argument, make s' const char *. (grub_arg_parse): Parse long options in place, not in a temporary buffer.
---
lib/arg.c | 37 ++++++++++--------------------------- 1 files changed, 10 insertions(+), 27 deletions(-)
lib/arg.c +10 -27
@@ -76,21 +76,8 @@ find_short (const struct grub_arg_option *options, char c)return found; } -static char * -find_long_option (char *s) -{ -char *argpos =grub_strchr (s,'=' ); - -if (argpos) - { - *argpos ='\0' ; -return ++argpos; - } -return 0 ; -} -static struct grub_arg_option * -find_long (const struct grub_arg_option *options,char *s) +find_long (const struct grub_arg_option *options,const char *s,int len) {struct grub_arg_option *found =0 ; autostruct grub_arg_option *fnd_long (const struct grub_arg_option *opt);
@@ -99,7 +86,7 @@ find_long (const struct grub_arg_option *options, char *s) {while (opt->doc) { -if (opt->longarg && !grub_strcmp (opt->longarg, s)) +if (opt->longarg && !grub_strncmp (opt->longarg, s, len))return (struct grub_arg_option *) opt; opt++; }
@@ -244,7 +231,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, struct grub_arg_list *usr, char ***args, int *argnum) { int curarg; -char *longarg =0 ; +int arglen;int complete =0 ;char **argl =0 ;int num =0 ;
@@ -328,14 +315,14 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,break ; } - longarg = (char *)grub_strdup (arg); -if (! longarg) - goto fail; + option =grub_strchr (arg,'=' ); +if (option) { + arglen = option - arg -2 ; + option++; + }else + arglen =grub_strlen (arg) -2 ; - option =find_long_option (longarg); - arg = longarg; - - opt =find_long (cmd->options, arg +2 ); + opt =find_long (cmd->options, arg +2 , arglen);if (! opt) {grub_error (GRUB_ERR_BAD_ARGUMENT ,"Unknown argument `%s'\n" , arg);
@@ -402,8 +389,6 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,if (parse_option (cmd, opt->shortarg,0 , usr) || grub_errno) goto fail; } -grub_free (longarg); - longarg =0 ; } complete =1 ;
@@ -412,7 +397,5 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv, *argnum = num; fail: -grub_free (longarg); -return complete; }
lib/arg.c find_long (const struct grub_arg_option *options, char *s) · line 89
Quoted 2 lines · Pavel Roskin
lib/arg.c +1
-1-if (opt->longarg && !grub_strcmp (opt->longarg, s)) +if (opt->longarg && !grub_strncmp (opt->longarg, s, len))
This change has an unexpected side effect, as GRUB starts recognizing abbreviated options (like --fs instead of --fs-uuid). But it would be wrong to add features in a patch that fixes bug. Besides, proper support for abbreviations should check that the abbreviation is unique. If it's not, only exact matches should be allowed.
So I'm changing it to:
if (opt->longarg && ! grub_strncmp (opt->longarg, s, len) &&
opt->longarg[len] == '\0')
Committed now.
Signature · 2 lines
Regards, Pavel Roskin