Commit Graph

15 Commits

Author SHA1 Message Date
Peter Jones f725fa7cb2 calloc: Use calloc() at most places
This modifies most of the places we do some form of:

  X = malloc(Y * Z);

to use calloc(Y, Z) instead.

Among other issues, this fixes:
  - allocation of integer overflow in grub_png_decode_image_header()
    reported by Chris Coulson,
  - allocation of integer overflow in luks_recover_key()
    reported by Chris Coulson,
  - allocation of integer overflow in grub_lvm_detect()
    reported by Chris Coulson.

Fixes: CVE-2020-14308

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 ad4bfeec5c Change fs functions to add fs_ prefix
This avoid conflict with gnulib

Signed-off-by: Vladimir Serbinenko <phcoder@google.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2019-04-09 10:03:29 +10:00
Colin Watson fc524edf65 Remove nested functions from filesystem directory iterators.
* include/grub/fs.h (grub_fs_dir_hook_t): New type.
(struct grub_fs.dir): Add hook_data argument.

Update all implementations and callers.
2013-01-21 01:33:46 +00: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 4e46148696 * grub-core/kern/fs.c (grub_fs_probe): Handle GRUB_ERR_OUT_OF_RANGE as
a bad FS.
2012-05-21 22:02:56 +02:00
Vladimir 'phcoder' Serbinenko bfb320c644 * grub-core/kern/fs.c (grub_fs_probe): Handle GRUB_ERR_OUT_OF_RANGE as
a bad FS.
2012-05-13 20:23:02 +02:00
Vladimir 'phcoder' Serbinenko 076aeb5022 * grub-core/kern/fs.c (grub_fs_probe) [GRUB_UTIL]: Add workaround for
btrfs.
2012-05-04 00:36:23 +02: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
Manoel R. Abranches 7dd64f1236 Use a net fs struct to handle open, reand and close in file. 2011-05-27 00:22:35 -03:00
Manoel Rebelo Abranches 25f1579b43 Adapt protocols to new network struct. 2011-04-01 05:42:34 -03:00
Vladimir 'phcoder' Serbinenko 9a9852df79 Hook network protocols 2010-09-02 00:07:55 +02:00
Vladimir 'phcoder' Serbinenko 975cffff74 merge mainline into net 2010-09-01 23:28:02 +02:00
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