Package: makedepf90
Version: 3.0.1-3
Severity: minor
Tags: patch
X-Debbugs-Cc: [email protected]

Four more out-of-bounds accesses, the last of those I found under
AddressSanitizer (see also #1155574 and my report on #if nesting, long
paths and -b). These need a non-default -m, -s, -r or -R, which comes from
the build script rather than the source, hence severity minor:

1. %f in -m/-s on a source name with no '.': the backward scan for the
suffix has no lower bound and reads, then writes, before the buffer.
ASan: heap-buffer-overflow, modfile_name.c:56

2. %s/%S in -s: the loop tests modulename[j] but copies submodulename[j],
so a parent name longer than the submodule name over-reads (and a
shorter one truncates).
ASan: heap-buffer-overflow read, modfile_name.c:71

3. A -m/-s format ending in '%' steps onto the NUL and the loop then
reads past it.
ASan: heap-buffer-overflow read, modfile_name.c:47

4. expand_rule() writes into a fixed RULE_LENGTH buffer. The 2.8.9 length
check covers only the separate "-r RULE" form; the joined "-rRULE"
form and -R skip it, and each %f expands to the source name.
ASan: heap-buffer-overflow write, utils.c:142

Reproducers:

printf 'module m\nend module\n' > m; cp m m.f90
printf 'submodule (parentlongname) s\nend\n' > sub.f90
makedepf90 -m %f.mod m
makedepf90 -s '%s.smod' sub.f90
makedepf90 -m 'x%' m.f90
makedepf90 -r$(python3 -c "print('A'*1100)") m.f90

Each fix is a few lines and leaves dependency output unchanged. Patches
against 3.0.1 below, also attached as DEP-3 patches. Each applies alone on
top of std23.patch and the #1155574 patch, or after the patches from my
previous report:

--- a/modfile_name.c
+++ b/modfile_name.c
@@ -58,8 +58,9 @@ char *modfile_name (const char *modulename,
for (j = 0; filename[j]; j++)
modfile[mi++] = filename[j];

- /* Skip the file name extension */
- for (mi--; modfile[mi] != '.'; mi--);
+ /* Skip the file name extension, if there is one */
+ for (j = mi - 1; j >= 0 && modfile[j] != '.'; j--);
+ if (j >= 0) mi = j;
break;
case 'm':
for (j = 0; modulename[j]; j++)

--- a/modfile_name.c
+++ b/modfile_name.c
@@ -74,7 +74,7 @@ char *modfile_name (const char *modulename,
if (!submodulename) {
warning("Submodule component not defined in this context");
} else {
- for (j = 0; modulename[j]; j++)
+ for (j = 0; submodulename[j]; j++)
modfile[mi++] = tolower (submodulename[j]);
}
break;
@@ -82,7 +82,7 @@ char *modfile_name (const char *modulename,
if (!submodulename) {
warning("Submodule component not defined in this context");
} else {
- for (j = 0; modulename[j]; j++)
+ for (j = 0; submodulename[j]; j++)
modfile[mi++] = toupper (submodulename[j]);
}
break;

--- a/modfile_name.c
+++ b/modfile_name.c
@@ -89,6 +89,10 @@ char *modfile_name (const char *modulename,
case '%':
modfile[mi++] = '%';
break;
+ case '\0':
+ warning ("Trailing '%%' in modfile format '%s'", fmt);
+ i--; /* so the loop stops at the NUL */
+ break;
default:
warning ("Unknown modifier '%%%c' in modfile format '%s'",
fmt[i], fmt);

--- a/utils.c
+++ b/utils.c
@@ -124,9 +124,8 @@ char *remove_citation(const char *s)
char *expand_rule(const char *r, const char *srcfile)
{
char *rule;
- int i, j, k, rlen, slen;
+ int i, j, k, rlen, slen, npct;

- rule = (char *)xmalloc(RULE_LENGTH*sizeof(char));
rlen = strlen(r);

/* set slen = length of srcfile without suffix */
@@ -135,6 +134,10 @@ char *expand_rule(const char *r, const char *srcfile)
/* If there was no suffix, set slen = strlen(srcfile) */
if (slen == 0) slen = strlen(srcfile);

+ /* Room for the tab, the rule, every '%' expanded to srcfile, and NUL */
+ for (i = npct = 0; i < rlen; i++) if (r[i] == '%') npct++;
+ rule = (char *)xmalloc(rlen + npct*slen + 2);
+
rule[0] = '\t';
k = 1;
for (i = 0; i < rlen; i++) {

Thanks for maintaining this.
Description: Do not scan before the module-file name buffer when %f has no suffix
 After copying the source file name for %f, modfile_name() searches backwards
 for '.' to strip the suffix with no lower bound.  If no '.' has been written
 (for example a source file named without a suffix), it reads, and then writes,
 before the start of the heap buffer.  Reproduced with AddressSanitizer:
 heap-buffer-overflow one byte before the buffer at modfile_name.c.
 .
 The scan now stops at the start of the buffer; with no '.', the whole name is
 kept, as replace_suffix() already does.  Output is unchanged whenever a '.' is
 present.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/modfile_name.c
+++ b/modfile_name.c
@@ -58,8 +58,9 @@ char *modfile_name (const char *modulename,
                     for (j = 0; filename[j]; j++)
                         modfile[mi++] = filename[j];
 
-                    /* Skip the file name extension */
-                    for (mi--; modfile[mi] != '.'; mi--);
+                    /* Skip the file name extension, if there is one */
+                    for (j = mi - 1; j >= 0 && modfile[j] != '.'; j--);
+                    if (j >= 0)  mi = j;
                     break;
                 case 'm':
                     for (j = 0; modulename[j]; j++)
Description: Copy the submodule name for %s/%S using its own length
 The %s and %S cases of modfile_name() loop while modulename[j] is non-zero but
 copy submodulename[j].  For a submodule the module name is the parent's, so a
 parent name longer than the submodule name reads past the submodule name, and
 a shorter one truncates it.  Reproduced with AddressSanitizer:
 heap-buffer-overflow read at modfile_name.c.
 .
 Only formats using %s or %S are affected; the default submodule format "%f.o"
 is not.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/modfile_name.c
+++ b/modfile_name.c
@@ -74,7 +74,7 @@ char *modfile_name (const char *modulename,
 		    if (!submodulename) {
 		      warning("Submodule component not defined in this context");
 		    } else  {
-		      for (j = 0; modulename[j]; j++)
+		      for (j = 0; submodulename[j]; j++)
                         modfile[mi++] = tolower (submodulename[j]);
 		    }
 		    break;
@@ -82,7 +82,7 @@ char *modfile_name (const char *modulename,
 		    if (!submodulename) {
 		      warning("Submodule component not defined in this context");
 		    } else  {
-		      for (j = 0; modulename[j]; j++)
+		      for (j = 0; submodulename[j]; j++)
                         modfile[mi++] = toupper (submodulename[j]);
 		    }
                     break;
Description: Stop at a trailing '%' in the -m/-s format instead of reading past its end
 When the -m or -s format ends in '%', modfile_name() steps onto the
 terminating NUL, warns about an unknown modifier, and the loop increment then
 reads past the end of the string.  Reproduced with AddressSanitizer:
 heap-buffer-overflow read at modfile_name.c.
 .
 A trailing '%' now gets its own warning and ends the format.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/modfile_name.c
+++ b/modfile_name.c
@@ -89,6 +89,10 @@ char *modfile_name (const char *modulename,
                 case '%':
                     modfile[mi++] = '%';
                     break;
+                case '\0':
+                    warning ("Trailing '%%' in modfile format '%s'", fmt);
+                    i--;  /* so the loop stops at the NUL */
+                    break;
                 default:
                     warning ("Unknown modifier '%%%c' in modfile format '%s'",
                              fmt[i], fmt);
Description: Size the expanded -r/-R rule buffer from its inputs to prevent a heap overflow
 expand_rule() expands a -r or -R rule into a fixed RULE_LENGTH (1024) byte
 heap buffer.  The length check added in 2.8.9 only tests the raw argument of
 the separate "-r RULE" form: the joined "-rRULE" form and -R are
 not checked, and each %f expands to the source file name, so a short rule can
 still overflow.  Reproduced with AddressSanitizer: heap-buffer-overflow write
 at utils.c, expand_rule.
 .
 The buffer is now allocated for the rule with every '%' expanded to the source
 name.  Output is unchanged; the existing RULE_LENGTH check is left in place.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/utils.c
+++ b/utils.c
@@ -124,9 +124,8 @@ char *remove_citation(const char *s)
 char *expand_rule(const char *r, const char *srcfile)
 {
     char *rule;
-    int i, j, k, rlen, slen;
+    int i, j, k, rlen, slen, npct;
 
-    rule = (char *)xmalloc(RULE_LENGTH*sizeof(char));
     rlen = strlen(r);
 
     /* set slen = length of srcfile without suffix */
@@ -135,6 +134,10 @@ char *expand_rule(const char *r, const char *srcfile)
     /* If there was no suffix, set slen = strlen(srcfile) */
     if (slen == 0) slen = strlen(srcfile);
 
+    /* Room for the tab, the rule, every '%' expanded to srcfile, and NUL */
+    for (i = npct = 0; i < rlen; i++)  if (r[i] == '%')  npct++;
+    rule = (char *)xmalloc(rlen + npct*slen + 2);
+
     rule[0] = '\t';
     k = 1;
     for (i = 0; i < rlen; i++) {

Reply via email to