[PATCH kernel-4.19 1/4] modpost: add read_text_file() and get_line() helpers

From: Masahiro Yamada <masahiroy@kernel.org> mainline inclusion from mainline-5.8 commit ac5100f54329676469688d1b5415cd8d6428c909 category: bugfix bugzilla: 46847 CVE: NA ----------------------------------------------- modpost uses grab_file() to open a file, but it is not suitable for a text file because the mmap'ed file is not terminated by null byte. Actually, I see some issues for the use of grab_file(). The new helper, read_text_file() loads the whole file content into a malloc'ed buffer, and appends a null byte. Then, get_line() reads each line. To handle text files, I intend to replace as follows: grab_file() -> read_text_file() get_new_line() -> get_line() Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Signed-off-by: Wang Wensheng <wangwensheng4@huawei.com> Reviewed-by: Jian Cheng <cj.chengjian@huawei.com> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com> --- scripts/mod/modpost.c | 49 +++++++++++++++++++++++++++++++++++++++++++ scripts/mod/modpost.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 91a80036c05d..99334521909b 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -118,6 +118,55 @@ void *do_nofail(void *ptr, const char *expr) return ptr; } +char *read_text_file(const char *filename) +{ + struct stat st; + size_t nbytes; + int fd; + char *buf; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + perror(filename); + exit(1); + } + + if (fstat(fd, &st) < 0) { + perror(filename); + exit(1); + } + + buf = NOFAIL(malloc(st.st_size + 1)); + + nbytes = st.st_size; + + while (nbytes) { + ssize_t bytes_read; + + bytes_read = read(fd, buf, nbytes); + if (bytes_read < 0) { + perror(filename); + exit(1); + } + + nbytes -= bytes_read; + } + buf[st.st_size] = '\0'; + + close(fd); + + return buf; +} + +char *get_line(char **stringp) +{ + /* do not return the unwanted extra line at EOF */ + if (*stringp && **stringp == '\0') + return NULL; + + return strsep(stringp, "\n"); +} + /* A list of all modules we processed */ static struct module *modules; diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 8453d6ac2f77..ff809119f82b 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -185,6 +185,8 @@ void maybe_frob_rcs_version(const char *modfilename, void get_src_version(const char *modname, char sum[], unsigned sumlen); /* from modpost.c */ +char *read_text_file(const char *filename); +char *get_line(char **stringp); void *grab_file(const char *filename, unsigned long *size); char* get_next_line(unsigned long *pos, void *file, unsigned long size); void release_file(void *file, unsigned long size); -- 2.25.1

From: Masahiro Yamada <masahiroy@kernel.org> mainline inclusion from mainline-5.8 commit 70f30cfe5b892fcb7f98e7df72ed6ccfe3225628 category: bugfix bugzilla: 46847 CVE: NA ------------------------------------------------ grab_file() mmaps a file, but it is not so efficient here because get_next_line() copies every line to the temporary buffer anyway. read_text_file() and get_line() are simpler. get_line() exploits the library function strchr(). Going forward, the missing *.symvers or *.cmd is a fatal error. This should not happen because scripts/Makefile.modpost guards the -i option files with $(wildcard $(input-symdump)). Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Drop the changes in modpost.c since the missing *.symvers or *.cmd is a fatal error and we don't check it in scripts/Makefile.modpost currently, on which curimistance the building process for modules would be interrupted with an error. This isn't what we expect. Signed-off-by: Wang Wensheng <wangwensheng4@huawei.com> Reviewed-by: Jian Cheng <cj.chengjian@huawei.com> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com> --- scripts/mod/sumversion.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c index 0f6dcb4011a8..b7b52350d08e 100644 --- a/scripts/mod/sumversion.c +++ b/scripts/mod/sumversion.c @@ -304,9 +304,8 @@ static int is_static_library(const char *objfile) * to figure out source files. */ static int parse_source_files(const char *objfile, struct md4_ctx *md) { - char *cmd, *file, *line, *dir; + char *cmd, *file, *line, *dir, *pos; const char *base; - unsigned long flen, pos = 0; int dirlen, ret = 0, check_files = 0; cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd"))); @@ -324,14 +323,12 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md) strncpy(dir, objfile, dirlen); dir[dirlen] = '\0'; - file = grab_file(cmd, &flen); - if (!file) { - warn("could not find %s for %s\n", cmd, objfile); - goto out; - } + file = read_text_file(cmd); + + pos = file; /* Sum all files in the same dir or subdirs. */ - while ((line = get_next_line(&pos, file, flen)) != NULL) { + while ((line = get_line(&pos))) { char* p = line; if (strncmp(line, "source_", sizeof("source_")-1) == 0) { @@ -382,8 +379,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md) /* Everyone parsed OK */ ret = 1; out_file: - release_file(file, flen); -out: + free(file); free(dir); free(cmd); return ret; -- 2.25.1

From: "H. Nikolaus Schaller" <hns@goldelico.com> mainline inclusion from mainline-5.8 commit 736bb11898ef748da1d805f40d485b66ceac9a3c category: bugfix bugzilla: 46847 CVE: NA ----------------------------------------------- strsep() is neither standard C nor POSIX and used outside the kernel code here. Using it here requires that the build host supports it out of the box which is e.g. not true for a Darwin build host and using a cross-compiler. This leads to: scripts/mod/modpost.c:145:2: warning: implicit declaration of function 'strsep' [-Wimplicit-function-declaration] return strsep(stringp, "\n"); ^ and a segfault when running MODPOST. See also: https://stackoverflow.com/a/7219504 So let's replace this by strchr() instead of using strsep(). It does not hurt kernel size or speed since this code is run on the build host. Fixes: ac5100f5432967 ("modpost: add read_text_file() and get_line() helpers") Co-developed-by: Masahiro Yamada <masahiroy@kernel.org> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Signed-off-by: Wang Wensheng <wangwensheng4@huawei.com> Reviewed-by: Jian Cheng <cj.chengjian@huawei.com> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com> --- scripts/mod/modpost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 99334521909b..b476d84feda7 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -160,11 +160,19 @@ char *read_text_file(const char *filename) char *get_line(char **stringp) { + char *orig = *stringp, *next; + /* do not return the unwanted extra line at EOF */ - if (*stringp && **stringp == '\0') + if (!orig || *orig == '\0') return NULL; - return strsep(stringp, "\n"); + next = strchr(orig, '\n'); + if (next) + *next++ = '\0'; + + *stringp = next; + + return orig; } /* A list of all modules we processed */ -- 2.25.1

From: Wolfram Sang <wsa@kernel.org> mainline inclusion from mainline-5.8 commit 6020db504cece0d93cc31c6f73609ef1304607e2 category: bugfix bugzilla: 46847 CVE: NA ----------------------------------------------- Mention why we open-code strsep, so it is clear that it is intentional. Fixes: 736bb11898ef ("modpost: remove use of non-standard strsep() in HOSTCC code") Signed-off-by: Wolfram Sang <wsa@kernel.org> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org> Signed-off-by: Wang Wensheng <wangwensheng4@huawei.com> Reviewed-by: Jian Cheng <cj.chengjian@huawei.com> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com> --- scripts/mod/modpost.c | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index b476d84feda7..683c95731150 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -166,6 +166,7 @@ char *get_line(char **stringp) if (!orig || *orig == '\0') return NULL; + /* don't use strsep here, it is not available everywhere */ next = strchr(orig, '\n'); if (next) *next++ = '\0'; -- 2.25.1
participants (1)
-
Yang Yingliang