Subject: [alpine-devel] [PATCH] Fix util-linux segfault when reading /proc/swaps

From: Jeff Pohlmeyer <[email-masked]>
Date: Wed, 4 Dec 2013 08:53:06 -0600

I was getting a segfault with "swapon -s" and "swapoff -a" and after some
searching I found a bug report[1] that explains the problem is in libmount
and affects some other util-linux programs as well. After rebuilding with
an updated patch[2] the problem seems to be fixed. The new patch looks like
it might have a few other changes, but I didn't dig too deeply into it.

- Jeff

[1] https://bugs.busybox.net/show_bug.cgi?id=6428
[2] http://git.buildroot.net/buildroot/tree/package/util-linux/util-linux-001-sscanf-no-ms-as.patch?id=62d15df19a0d181a266a88b46525d0d4959c8739
diff --git a/main/util-linux/APKBUILD b/main/util-linux/APKBUILD
index 95934ee..c7f37fc 100644
--- a/main/util-linux/APKBUILD
+++ b/main/util-linux/APKBUILD
@@ -8,7 +8,7 @@ case $pkgver in
 	*.*) _v=$pkgver;;
 esac
 
-pkgrel=3
+pkgrel=4
 pkgdesc="Random collection of Linux utilities"
 url="http://kernel.org/~kzak/util-linux/"
 arch="all"
@@ -124,16 +124,16 @@ mcookie() {
 
 md5sums="b39fde897334a4858bb2098edcce5b3f  util-linux-2.23.2.tar.xz
 9682a6ddd5abe65434a145ebe512c123  program-invocation.patch
-85125e2cc7d54dc2733bedc603886f81  gentoo-no-scanf.patch
+da609e7f045bec81e10868fa66d649c6  gentoo-no-scanf.patch
 43a03270b6e49fdf4c7fb0ab3390c242  util-linux-posix.patch
 6196f1ce853dfaf717569c1e35555d6d  ttydefaults.h"
 sha256sums="7c4042bd91f621250d7b3b2f34e3b11921a32c7f080c149dcc0c7ce62a8a7cac  util-linux-2.23.2.tar.xz
 2bfa27969effc77631e981bbbcd1bf261e61f3e51875db950883ddb2a90ea4fd  program-invocation.patch
-2a2429b59151a63a75d2ff46f60732c6a3fbe9b52e472802edcf4eedbe3e22f2  gentoo-no-scanf.patch
+2843a637f8300720b0f0917496d7b814d210a8777c93cfac51fcd3e5777e21d5  gentoo-no-scanf.patch
 d099d398d5e6649276d0ccd3b276aa762f52efec8295706370fef44287a2386f  util-linux-posix.patch
 46faf1198bd884d12c5d45019a5fec8dfdefeae6721d8c9f3da89921acdb2a6d  ttydefaults.h"
 sha512sums="dcccf8e3a3892a2ac98eea0a7e1c24c1b612ef10b466a8b34d8ebe6c2d49778bc9e8bfb1866ce0daa6db387396659120c290c5b36f47864c4d513b5ed15f9618  util-linux-2.23.2.tar.xz
 5a07e86fa84961f73e53677d202e7a2101704eacb698426e62da897af07c7f1227e841f79ff59deed50cfb0e9ef1816f7c3fd03382ae4aa46909b6cd41a95d4e  program-invocation.patch
-5f199511d3c6919c1a23248f2fd1d15343ee330e169b8440ca48bbbbc1aae243d8bd652ab504520c5d12dd0875e5e94a704d727d4b71bcd940d4ff51115b3981  gentoo-no-scanf.patch
+b505f828d64567cf28590de15200e89b4ca26c562134db4c1bb8adf829a903a703d92d3597a5d86d1053ff602c41f84d88cf16b45ef7d857a021c73a9b7d0ee7  gentoo-no-scanf.patch
 4a5052caad3a9fb176283d49c486256453ede2a10d99c5f0a4e78a25a03f09a69c6d840756e07aaee581003b018c8b8279e6f727712edf03ff755d162544b729  util-linux-posix.patch
 876bb9041eca1b2cca1e9aac898f282db576f7860aba690a95c0ac629d7c5b2cdeccba504dda87ff55c2a10b67165985ce16ca41a0694a267507e1e0cafd46d9  ttydefaults.h"
diff --git a/main/util-linux/gentoo-no-scanf.patch b/main/util-linux/gentoo-no-scanf.patch
index 6818224..c95b755 100644
--- a/main/util-linux/gentoo-no-scanf.patch
+++ b/main/util-linux/gentoo-no-scanf.patch
@@ -1,19 +1,21 @@
-for systems that don't support latest POSIX standard: %as
+Fix libmount build under uClibc
 
-https://bugs.gentoo.org/406303
+See https://bugs.gentoo.org/show_bug.cgi?id=406303
+http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/sys-apps/util-linux/files/util-linux-2.21.1-no-printf-alloc.patch?revision=1.2
+ported to util-linux-2.23.2
 
---- a/configure.ac
-+++ b/configure.ac
-@@ -688,7 +688,6 @@ AC_ARG_ENABLE([libmount],
+--- a/configure.ac	2013-07-30 03:39:26.188738061 -0500
++++ b/configure.ac	2013-09-05 15:31:11.460864363 -0500
+@@ -755,7 +755,6 @@
  UL_BUILD_INIT([libmount])
  UL_REQUIRES_LINUX([libmount])
  UL_REQUIRES_BUILD([libmount], [libblkid])
 -UL_REQUIRES_HAVE([libmount], [scanf_alloc_modifier], [scanf string alloc modifier])
  AM_CONDITIONAL(BUILD_LIBMOUNT, test "x$build_libmount" = xyes)
+ AM_CONDITIONAL(BUILD_LIBMOUNT_TESTS, test "x$build_libmount" = xyes -a "x$enable_static" = xyes)
  
- AC_SUBST([LIBMOUNT_VERSION])
---- a/libmount/src/tab_parse.c
-+++ b/libmount/src/tab_parse.c
+--- a/libmount/src/tab_parse.c	2013-07-30 03:39:26.218738358 -0500
++++ b/libmount/src/tab_parse.c	2013-09-05 15:31:11.460864363 -0500
 @@ -22,6 +22,10 @@
  #include "pathnames.h"
  #include "strutils.h"
@@ -25,7 +27,7 @@ https://bugs.gentoo.org/406303
  static inline char *skip_spaces(char *s)
  {
  	assert(s);
-@@ -61,16 +65,31 @@ static int mnt_parse_table_line(struct libmnt_fs *fs, char *s)
+@@ -61,16 +65,31 @@
  	int rc, n = 0, xrc;
  	char *src = NULL, *fstype = NULL, *optstr = NULL;
  
@@ -57,7 +59,7 @@ https://bugs.gentoo.org/406303
  			&n);
  	xrc = rc;
  
-@@ -136,6 +155,16 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, char *s)
+@@ -136,6 +155,16 @@
  	unsigned int maj, min;
  	char *fstype = NULL, *src = NULL, *p;
  
@@ -74,7 +76,7 @@ https://bugs.gentoo.org/406303
  	rc = sscanf(s,	"%u "		/* (1) id */
  			"%u "		/* (2) parent */
  			"%u:%u "	/* (3) maj:min */
-@@ -147,9 +175,15 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, char *s)
+@@ -147,9 +176,15 @@
  			&fs->id,
  			&fs->parent,
  			&maj, &min,
@@ -90,7 +92,7 @@ https://bugs.gentoo.org/406303
  			&end);
  
  	if (rc >= 7 && end > 0)
-@@ -167,9 +201,15 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, char *s)
+@@ -169,9 +204,15 @@
  			UL_SCNsA" "	/* (9) source */
  			UL_SCNsA,	/* (10) fs options (fs specific) */
  
@@ -106,3 +108,29 @@ https://bugs.gentoo.org/406303
  
  	if (rc >= 10) {
  		fs->flags |= MNT_FS_KERNEL;
+@@ -279,14 +320,25 @@
+ 	int rc;
+ 	char *src = NULL;
+ 
++#ifndef HAVE_SCANF_MS_MODIFIER
++	size_t len = strlen(s) + 1;
++	src = malloc(len);
++	fs->swaptype = malloc(len);
++#endif
++
+ 	rc = sscanf(s,	UL_SCNsA" "	/* (1) source */
+ 			UL_SCNsA" "	/* (2) type */
+ 			"%jd"		/* (3) size */
+ 			"%jd"		/* (4) used */
+ 			"%d",		/* priority */
+ 
++#ifdef HAVE_SCANF_MS_MODIFIER
+ 			&src,
+ 			&fs->swaptype,
++#else
++			src,
++			fs->swaptype,
++#endif
+ 			&fsz,
+ 			&usz,
+ 			&fs->priority);

---

From: Natanael Copa <[email-masked]>
Date: Wed, 4 Dec 2013 16:06:01 +0100

On Wed, 4 Dec 2013 08:53:06 -0600
Jeff Pohlmeyer <[email-masked]> wrote:

> Fix util-linux segfault when reading /proc/swaps
applied. thanks!

-nc

---
Unsubscribe:  [email-masked]
Help:         [email-masked]
---