Hello,

I would like to report two independent security defects in GNU groff,
both verified against the
1.24.0 sources. They are unrelated to each other; I am sending them in
one message for convenience
and am happy to split them into separate Savannah entries if you
prefer. No publication date is set
and I am glad to coordinate timing.

================================================================================
(1) eqn: stack buffer overflow in interpolate_macro_with_args via
macro arguments
================================================================================
CWE-121 (Stack-based Buffer Overflow)
Component: interpolate_macro_with_args (src/preproc/eqn/lex.cpp)
Affected: groff 1.24.0 and earlier releases carrying the same eqn
lexer (1.23.0 included)
Vector: local / file-borne - a macro call in an .eqn or roff input
document (reachable via groff -e)
Impact: out-of-bounds stack write (of pointers) -> denial of service /
memory corruption

Root cause:

    /* src/preproc/eqn/lex.cpp:754 */  void
interpolate_macro_with_args(const char *body)
    /* :756 */  { char *argv[9];
    /* :757 */    int argc = 0;
                  for (int i = 0; i < 9; i++) argv[i] = 0;
                  do {
                    ...
                    if (level == 0 && (c == ',' || c == ')')) {
                      if (token_buffer.length() > 0) {
    /* :774 */          argv[argc] = strsave(token_buffer.contents());
  /* no argc < 9 guard */
                      }
                      if (argc > 0 || c != ')' || i > 0)
    /* :778 */          argc++;
                      break;
                    }
                    ...
                  } while (c != ')' && c != EOF);
                  current_input = new argument_macro_input(body, argc,
argv, current_input);
                }

The routine scans the comma-separated argument list of a macro call,
saving each argument into
argv[argc] and then incrementing argc, with no check that argc stays
below 9. argv is a fixed 9-slot
pointer array on the stack, so a macro invoked with ten or more
arguments writes argv[9], argv[10] and
beyond past the array. The over-count is also propagated into
argument_macro_input(body, argc, argv,
...): the constructor copies argc entries (lex.cpp:536-537) and
get/peek index argv[i] for i < argc,
compounding the out-of-bounds access.

Trigger (conceptual, no weaponised exploit): process a document that
defines an eqn macro and then
invokes it with ten or more arguments, e.g. a call of the form
m(a,b,c,d,e,f,g,h,i,j) inside an eqn
block. The tenth and later argument pointers are written past argv[9].

Suggested fix: bound argc against the array size - reject or diagnose
macro calls with more than nine
arguments - or collect the arguments into a dynamically sized container.

================================================================================
(2) pre-grohtml: OS command injection via the image_generator value in
devhtml/DESC
================================================================================
CWE-78 (OS Command Injection)
Component: the HTML image-generation pipeline in pre-grohtml
Affected: groff 1.24.0 (studied at commit
967814d0057ba2bd7802c81dae2bc0e7a4f2616e) and earlier
          releases carrying the same code
Vector: local - a device description file (devhtml/DESC) supplied through -F
Impact: arbitrary OS command execution with the privileges of the invoking user

Root cause: pre-grohtml reads the image_generator value from the
devhtml/DESC device description file
and concatenates it into a shell command string that is then executed
with system(). Because the
font/device path can be redirected with -F, the DESC file - and
therefore the image_generator value -
is attacker-influenced input rather than fixed installation data, and
nothing quotes or validates it
before it reaches the shell.

Trigger (conceptual, no weaponised exploit): point groff's font path
at a directory containing a
crafted devhtml/DESC whose image_generator line carries shell
metacharacters, then format a document
through the HTML pipeline; the metacharacters are interpreted by the
shell that system() spawns.

Suggested fix: replace the system() call with fork() plus execvp() and
an argument vector so the
configured generator name and its arguments are passed as data and
never parsed as shell syntax;
alternatively shell-quote and validate the value read from DESC.

I recognise that -F is an explicit operator action, so you may well
assess this as lower severity than
the eqn overflow. I am reporting it because the value crosses from a
data file into a shell command
unquoted, which seemed worth your judgement rather than mine.

================================================================================

I am happy to supply further detail or to test candidate patches.

Discoverer / Reporter: wheat MAX <[email protected]>

Reply via email to