sed(1) size_t overflow bug may cause memory allocation error.

Issue #133 resolved
Takehiko NOZAKI repo owner created an issue

process.c, the size of appends array allocation(appendx and appendnum) is potentially size_t overflow.

                               if (appendx >= appendnum) {
                                       appends = erealloc(appends,
                                           sizeof(struct s_appends) *
                                           (appendnum * 2));
                                       appendnum *= 2;
                               }

should be:

#define MAX_APPENDS (SIZE_MAX / sizeof(struct s_appends))
                               if (appendx >= appendnum) {
                                       if (appendnum > MAX_APPENDS / 2)
                                               errx(EXIT_FAILURE, "%s", strerror(ENOMEM));
                                       appendnum *= 2;
                                       appends = erealloc(appends,
                                           sizeof(struct s_appends) * appendnum );
                               }

OpenBSD seems use their own API xreallocarray(3) http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/sed/process.c#rev1.20 http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/sed/process.c#rev1.22

Comments (4)

  1. Log in to comment