From 61e5068603aa005811e271c8198c4e3c44c3b6c8 Mon Sep 17 00:00:00 2001 From: Jaromir Capik Date: Tue, 16 Apr 2013 17:18:25 +0200 Subject: [PATCH] - Avoid segfaults when reading zero bytes - file2str (#951391) --- ...-3.3.7-segv-file2str-read-zero-bytes.patch | 41 +++++++++++++++++++ procps-ng.spec | 7 +++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 procps-ng-3.3.7-segv-file2str-read-zero-bytes.patch diff --git a/procps-ng-3.3.7-segv-file2str-read-zero-bytes.patch b/procps-ng-3.3.7-segv-file2str-read-zero-bytes.patch new file mode 100644 index 0000000..3cfad63 --- /dev/null +++ b/procps-ng-3.3.7-segv-file2str-read-zero-bytes.patch @@ -0,0 +1,41 @@ +diff --git a/proc/readproc.c b/proc/readproc.c +--- procps-ng-3.3.7.orig/proc/readproc.c 2013-03-25 11:50:21.000000000 +0100 ++++ procps-ng-3.3.7/proc/readproc.c 2013-03-26 17:50:54.860228114 +0100 +@@ -534,28 +534,32 @@ + + static int file2str(const char *directory, const char *what, struct utlbuf_s *ub) { + #define readMAX 4096 +- #define buffMIN (tot_read + num + 1) // +1 for the '\0' delimiter ++ #define buffMIN (tot_read + num + 1) // +1 for the '\0' delimiter ++ #define buffGRW (30 + (buffMIN * 5) / 4) // grow by more than 25% + char path[PROCPATHLEN], chunk[readMAX]; +- int fd, num, tot_read = 0; ++ int fd, num, eof = 0, tot_read = 0; + + /* on first use we preallocate a buffer of minimum size to emulate + former 'local static' behavior -- even if this read fails, that +- buffer will likely soon be used for another sudirectory anyway */ ++ buffer will likely soon be used for another subdirectory anyway */ + if (ub->buf) ub->buf[0] = '\0'; + else ub->buf = xcalloc((ub->siz = readMAX)); + sprintf(path, "%s/%s", directory, what); + if (-1 == (fd = open(path, O_RDONLY, 0))) return -1; +- while (0 < (num = read(fd, chunk, readMAX))) { ++ while (!eof && 0 < (num = read(fd, chunk, readMAX))) { + if (ub->siz < buffMIN) +- ub->buf = xrealloc(ub->buf, (ub->siz = buffMIN)); ++ ub->buf = xrealloc(ub->buf, (ub->siz = buffGRW)); + memcpy(ub->buf + tot_read, chunk, num); + tot_read += num; ++ eof = (num < readMAX); + }; + ub->buf[tot_read] = '\0'; + close(fd); ++ if (unlikely(tot_read < 1)) return -1; + return tot_read; + #undef readMAX + #undef buffMIN ++ #undef buffGRW + } + + static char** file2strvec(const char* directory, const char* what) { diff --git a/procps-ng.spec b/procps-ng.spec index 1220890..3ceab9f 100644 --- a/procps-ng.spec +++ b/procps-ng.spec @@ -3,7 +3,7 @@ Summary: System and process monitoring utilities Name: procps-ng Version: 3.3.7 -Release: 2%{?dist} +Release: 3%{?dist} License: GPLv2+ and LGPLv2+ Group: Applications/System URL: https://sourceforge.net/projects/procps-ng/ @@ -11,6 +11,7 @@ URL: https://sourceforge.net/projects/procps-ng/ Source: http://downloads.sourceforge.net/%{name}/%{name}-%{version}.tar.xz Patch0: procps-ng-3.3.7-testsuite-reverting-bogus-commit-unix-exp.patch +Patch1: procps-ng-3.3.7-segv-file2str-read-zero-bytes.patch Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig @@ -67,6 +68,7 @@ System and process monitoring utilities development headers %setup -q -n %{name}-%{version} %patch0 -p1 +%patch1 -p1 %build # The following stuff is needed for git archives only @@ -129,6 +131,9 @@ mkdir -p %{buildroot}%{_sysconfdir}/sysctl.d %{_includedir}/proc %changelog +* Tue Apr 16 2013 Jaromir Capik - 3.3.7-3 +- Avoid segfaults when reading zero bytes - file2str (#951391) + * Mon Apr 15 2013 Jaromir Capik - 3.3.7-2 - Moving libprocps.pc to the devel subpackage (#951726)