Skip to contentSkip to conversation

libmount: avoid endless loop in, mnt_get_kernel_cmdline_option

PATCH In review Bernhard Voelker 2 participants 3 messages Nov 30, 2012 – Dec 10, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmount: avoid endless loop in, mnt_get_kernel_cmdline_option

Quoted 1 line

From 4ddf21607847570e1da3b692276f2a8242bd5af6 Mon Sep 17 00:00:00 2001

From: Bernhard Voelker <[email protected]>
Date: Fri, 30 Nov 2012 17:12:12 +0100
Subject: [PATCH] libmount: avoid endless loop in
mnt_get_kernel_cmdline_option

The above function infloops when the name to search for can only be found at the beginning of /proc/cmdline but doesn't match, e.g. when searching for "ro" in "root=/dev/sda1 quiet vga=0x31a".

* libmount/src/utils.c (mnt_get_kernel_cmdline_option): Replace while by for loop to ensure the pointer p is incremented.

---
 libmount/src/utils.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
libmount/src/utils.c +1 -3
index 74c3ad5..be242d0 100644
@@ -947,9 +947,7 @@ char *mnt_get_kernel_cmdline_option(const char *name)
 	if (len && *(name + len - 1) == '=')
 		val = 1;
 
-	while (p && *p) {
-		if (p != buf)
-			p++;
+	for ( ; p && *p; p++) {
 		if (!(p = strstr(p, name)))
 			break;			/* not found the option */
 		if (p != buf && !isblank(*(p - 1)))
Signature · 1 line
1.7.7
#1 lore raw
Quoted 5 lines · Bernhard Voelker

From 4ddf21607847570e1da3b692276f2a8242bd5af6 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <[email protected]>
Date: Fri, 30 Nov 2012 17:12:12 +0100
Subject: [PATCH] libmount: avoid endless loop in
mnt_get_kernel_cmdline_option

Hi Karel,

BTW: the #ifdef in mnt_get_kernel_cmdline_option doesn't seem to work to make use of the LIBMOUNT_KERNEL_CMDLINE env var:

output/libmount/utils-kernel-cmdline 2012-12-04 16:17:49.629199061 +0100 renamed from expected/libmount/utils-kernel-cmdline 2012-11-22 10:41:18.424077009 +0100 +3 -3
@@ -1,5 +1,5 @@
-'selinux=' found, argument: '0'
+'selinux=' not found
 'selinux' not found
-'ro' found
+'ro' not found
 'ro=' not found
-'root=' found, argument: 'UUID=33230ae2-1093-4353-824c-f7ca09a2a882'
+'root=' found, argument: '/dev/sda1'

And ltraced:

 $ export LIBMOUNT_KERNEL_CMDLINE=`pwd`/tests/ts/libmount/files/kernel_cmdline
 $ ltrace -e getenv,fopen -f ./test_mount_utils  --kernel-cmdline selinux=
 getenv("LIBMOUNT_DEBUG")                                                                    = NULL
 fopen("/proc/cmdline", "r")                                                                 = 0x609010
 'selinux=' not found
 +++ exited (status 0) +++

I.e. the utils test may only work on your PC. ;-)

BTW2: the libmount tests all fail when the user has set LIBMOUNT_DEBUG=0xffff, because the produced output differs. Maybe LIBMOUNT_DEBUG should be unset in tests/run.sh?

Have a nice day,
Berny

maintainer
#2 lore raw
Quoted 24 lines · Bernhard Voelker
Quoted 5 lines · Bernhard Voelker

From 4ddf21607847570e1da3b692276f2a8242bd5af6 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <[email protected]>
Date: Fri, 30 Nov 2012 17:12:12 +0100
Subject: [PATCH] libmount: avoid endless loop in
mnt_get_kernel_cmdline_option

Hi Karel,

BTW: the #ifdef in mnt_get_kernel_cmdline_option doesn't seem to work to make use of the LIBMOUNT_KERNEL_CMDLINE env var:

output/libmount/utils-kernel-cmdline 2012-12-04 16:17:49.629199061 +0100 renamed from expected/libmount/utils-kernel-cmdline 2012-11-22 10:41:18.424077009 +0100 +3 -3
@@ -1,5 +1,5 @@
-'selinux=' found, argument: '0'
+'selinux=' not found
 'selinux' not found
-'ro' found
+'ro' not found
 'ro=' not found
-'root=' found, argument: 'UUID=33230ae2-1093-4353-824c-f7ca09a2a882'
+'root=' found, argument: '/dev/sda1'
Quoted 8 lines

And ltraced:

 $ export LIBMOUNT_KERNEL_CMDLINE=`pwd`/tests/ts/libmount/files/kernel_cmdline
 $ ltrace -e getenv,fopen -f ./test_mount_utils  --kernel-cmdline selinux=
 getenv("LIBMOUNT_DEBUG")                                                                    = NULL
 fopen("/proc/cmdline", "r")                                                                 = 0x609010
 'selinux=' not found
 +++ exited (status 0) +++

It works, the problem is safe_getenv(), it requires root permissions.

I had the test originally without the #ifdef TEST_PROGRAM, so the safe_getenv() was good idea, now it's overkill. Fixed, thanks!

 $ LIBMOUNT_KERNEL_CMDLINE=tests/ts/libmount/files/kernel_cmdline \
   ltrace -e fopen,getenv ./test_mount_utils --kernel-cmdline selinux=

getenv("LIBMOUNT_DEBUG") = NULL
getenv("LIBMOUNT_KERNEL_CMDLINE") = "tests/ts/libmount/files/kernel_c"...
fopen("tests/ts/libmount/files/kernel_c"..., "r") = 0x1c99010

Quoted 1 line

I.e. the utils test may only work on your PC. ;-)

:-)

Quoted 3 lines

BTW2: the libmount tests all fail when the user has set LIBMOUNT_DEBUG=0xffff, because the produced output differs. Maybe LIBMOUNT_DEBUG should be unset in tests/run.sh?

Good idea. Implemented.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com