Commit graph

20 commits

Author SHA1 Message Date
Peter Jones
3f05d693d1 malloc: Use overflow checking primitives where we do complex allocations
This attempts to fix the places where we do the following where
arithmetic_expr may include unvalidated data:

  X = grub_malloc(arithmetic_expr);

It accomplishes this by doing the arithmetic ahead of time using grub_add(),
grub_sub(), grub_mul() and testing for overflow before proceeding.

Among other issues, this fixes:
  - allocation of integer overflow in grub_video_bitmap_create()
    reported by Chris Coulson,
  - allocation of integer overflow in grub_png_decode_image_header()
    reported by Chris Coulson,
  - allocation of integer overflow in grub_squash_read_symlink()
    reported by Chris Coulson,
  - allocation of integer overflow in grub_ext2_read_symlink()
    reported by Chris Coulson,
  - allocation of integer overflow in read_section_as_string()
    reported by Chris Coulson.

Fixes: CVE-2020-14309, CVE-2020-14310, CVE-2020-14311

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-07-29 16:55:47 +02:00
Peter Jones
d5a32255de misc: Make grub_strtol() "end" pointers have safer const qualifiers
Currently the string functions grub_strtol(), grub_strtoul(), and
grub_strtoull() don't declare the "end" pointer in such a way as to
require the pointer itself or the character array to be immutable to the
implementation, nor does the C standard do so in its similar functions,
though it does require us not to change any of it.

The typical declarations of these functions follow this pattern:

long
strtol(const char * restrict nptr, char ** restrict endptr, int base);

Much of the reason for this is historic, and a discussion of that
follows below, after the explanation of this change.  (GRUB currently
does not include the "restrict" qualifiers, and we name the arguments a
bit differently.)

The implementation is semantically required to treat the character array
as immutable, but such accidental modifications aren't stopped by the
compiler, and the semantics for both the callers and the implementation
of these functions are sometimes also helped by adding that requirement.

This patch changes these declarations to follow this pattern instead:

long
strtol(const char * restrict nptr,
       const char ** const restrict endptr,
       int base);

This means that if any modification to these functions accidentally
introduces either an errant modification to the underlying character
array, or an accidental assignment to endptr rather than *endptr, the
compiler should generate an error.  (The two uses of "restrict" in this
case basically mean strtol() isn't allowed to modify the character array
by going through *endptr, and endptr isn't allowed to point inside the
array.)

It also means the typical use case changes to:

  char *s = ...;
  const char *end;
  long l;

  l = strtol(s, &end, 10);

Or even:

  const char *p = str;
  while (p && *p) {
	  long l = strtol(p, &p, 10);
	  ...
  }

This fixes 26 places where we discard our attempts at treating the data
safely by doing:

  const char *p = str;
  long l;

  l = strtol(p, (char **)&ptr, 10);

It also adds 5 places where we do:

  char *p = str;
  while (p && *p) {
	  long l = strtol(p, (const char ** const)&p, 10);
	  ...
	  /* more calls that need p not to be pointer-to-const */
  }

While moderately distasteful, this is a better problem to have.

With one minor exception, I have tested that all of this compiles
without relevant warnings or errors, and that /much/ of it behaves
correctly, with gcc 9 using 'gcc -W -Wall -Wextra'.  The one exception
is the changes in grub-core/osdep/aros/hostdisk.c , which I have no idea
how to build.

Because the C standard defined type-qualifiers in a way that can be
confusing, in the past there's been a slow but fairly regular stream of
churn within our patches, which add and remove the const qualifier in many
of the users of these functions.  This change should help avoid that in
the future, and in order to help ensure this, I've added an explanation
in misc.h so that when someone does get a compiler warning about a type
error, they have the fix at hand.

The reason we don't have "const" in these calls in the standard is
purely anachronistic: C78 (de facto) did not have type qualifiers in the
syntax, and the "const" type qualifier was added for C89 (I think; it
may have been later).  strtol() appears to date from 4.3BSD in 1986,
which means it could not be added to those functions in the standard
without breaking compatibility, which is usually avoided.

The syntax chosen for type qualifiers is what has led to the churn
regarding usage of const, and is especially confusing on string
functions due to the lack of a string type.  Quoting from C99, the
syntax is:

 declarator:
  pointer[opt] direct-declarator
 direct-declarator:
  identifier
  ( declarator )
  direct-declarator [ type-qualifier-list[opt] assignment-expression[opt] ]
  ...
  direct-declarator [ type-qualifier-list[opt] * ]
  ...
 pointer:
  * type-qualifier-list[opt]
  * type-qualifier-list[opt] pointer
 type-qualifier-list:
  type-qualifier
  type-qualifier-list type-qualifier
 ...
 type-qualifier:
  const
  restrict
  volatile

So the examples go like:

const char foo;			// immutable object
const char *foo;		// mutable pointer to object
char * const foo;		// immutable pointer to mutable object
const char * const foo;		// immutable pointer to immutable object
const char const * const foo; 	// XXX extra const keyword in the middle
const char * const * const foo; // immutable pointer to immutable
				//   pointer to immutable object
const char ** const foo;	// immutable pointer to mutable pointer
				//   to immutable object

Making const left-associative for * and right-associative for everything
else may not have been the best choice ever, but here we are, and the
inevitable result is people using trying to use const (as they should!),
putting it at the wrong place, fighting with the compiler for a bit, and
then either removing it or typecasting something in a bad way.  I won't
go into describing restrict, but its syntax has exactly the same issue
as with const.

Anyway, the last example above actually represents the *behavior* that's
required of strtol()-like functions, so that's our choice for the "end"
pointer.

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-28 12:41:29 +01:00
Vladimir Serbinenko
2622b1e8a8 * grub-core/lib/arg.c: Don't discard const attribute. 2013-12-21 13:28:52 +01:00
Vladimir Serbinenko
e14d409b6f * grub-core/lib/arg.c: Ensure at least a single space between commands. 2013-12-17 14:42:02 +01:00
Vladimir Serbinenko
40a5bfde36 * grub-core/lib/arg.c (grub_arg_parse): Remove nested function. 2013-11-07 02:46:23 +01:00
Vladimir 'phcoder' Serbinenko
e0aacf2433 * grub-core/lib/arg.c (grub_arg_show_help): Fix a NULL pointer
dereference.
	Reported by: qwertial.
2013-04-29 11:57:51 +02:00
Vladimir 'phcoder' Serbinenko
ea811130ea * grub-core/lib/arg.c (grub_arg_list_alloc): Use shifts rather
than divisions.
2013-03-10 20:08:15 +01:00
Vladimir 'phcoder' Serbinenko
ff2b3cd928 * grub-core/lib/arg.c (grub_arg_show_help): Move showargs
out of its parent.
2013-02-28 11:00:59 +01:00
Colin Watson
5c67ea6cd9 Remove several trivially-unnecessary uses of nested functions.
* grub-core/commands/i386/pc/sendkey.c
(grub_cmd_sendkey: find_key_code, find_ascii_code): Make static
instead of nested.
* grub-core/commands/legacycfg.c (legacy_file: getline): Likewise.
Rename to ...
(legacy_file_getline): ... this.
* grub-core/commands/loadenv.c (grub_cmd_load_env: set_var):
Likewise.
* grub-core/kern/corecmd.c (grub_core_cmd_set: print_env): Likewise.
* grub-core/kern/fs.c (grub_fs_probe: dummy_func): Likewise.  Rename
to ...
(probe_dummy_iter): ... this.
* grub-core/kern/i386/coreboot/mmap.c
(grub_linuxbios_table_iterate: check_signature): Likewise.
* grub-core/kern/parser.c (grub_parser_split_cmdline:
check_varstate): Likewise.  Mark inline.
* grub-core/lib/arg.c (find_short: fnd_short): Likewise.  Pass
an additional parameter.
(find_long: fnd_long): Likewise.  Pass two additional parameters.
* grub-core/lib/crc.c (init_crc32c_table: reflect): Likewise.
* grub-core/lib/crc64.c (init_crc64_table: reflect): Likewise.
* grub-core/lib/ieee1275/cmos.c (grub_cmos_find_port: hook):
Likewise.  Rename to ...
(grub_cmos_find_port_iter): ... this.
* grub-core/lib/ieee1275/datetime.c (find_rtc: hook): Likewise.
Rename to ...
(find_rtc_iter): ... this.

* grub-core/normal/menu_entry.c (run): Fold nested editor_getsource
function directly into the function body, since it is only called
once.
2012-12-31 17:31:38 +00:00
Vladimir 'phcoder' Serbinenko
9c4b5c13e6 Improve gettext support. Stylistic fixes and error handling fixes while
on it.
2012-02-08 19:26:01 +01:00
Seth Goldberg
7e532280bd * grub-core/lib/arg.c (grub_arg_parse): Fix NULL pointer dereference. 2012-01-12 22:20:29 +01:00
Vladimir 'phcoder' Serbinenko
aa68ca1250 simplify lib/arg.c and allow purely long options 2011-12-23 21:13:09 +01:00
Vladimir 'phcoder' Serbinenko
6428dec358 * grub-core/lib/arg.c (grub_arg_parse): Avoid interpreting direct
argument as an argument to no-argument option.
2010-11-01 12:45:51 +01:00
Vladimir 'phcoder' Serbinenko
44a1b4327a * grub-core/lib/arg.c (grub_arg_parse): Fix treating of all commands as
if they were BSD-style.
2010-09-29 22:48:38 +02:00
Vladimir 'phcoder' Serbinenko
41e9c57d89 * grub-core/lib/arg.c (grub_arg_show_help): Correctly handle
parameters overflow.
2010-09-20 16:12:15 +02:00
Vladimir 'phcoder' Serbinenko
79c4eeb919 Fix incorrect echo options handling.
Reported by: Yves Blusseau.

	* include/grub/command.h (grub_command_flags_t): New flags
	GRUB_COMMAND_ACCEPT_DASH and GRUB_COMMAND_OPTIONS_AT_START.
	* grub-core/lib/arg.c (grub_arg_parse): Handle new flags.
	* grub-core/commands/echo.c (GRUB_MOD_INIT): Use new flags.
2010-09-14 23:30:06 +02:00
BVK Chaitanya
47b4c2c3e8 pull-in block-arg branch 2010-08-26 11:32:35 +05:30
BVK Chaitanya
16c7cb32c8 merge with mainline 2010-08-19 16:54:00 +05:30
BVK Chaitanya
297f0c2b6e merge with mainline 2010-07-13 00:43:28 +05:30
BVK Chaitanya
8c41176882 automake commit without merge history 2010-05-06 11:34:04 +05:30
Renamed from lib/arg.c (Browse further)