linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jes Sorensen <Jes.Sorensen@redhat.com>
To: neilb@suse.de
Cc: linux-raid@vger.kernel.org, dledford@redhat.com
Subject: Re: [PATCH 19/19] make_parts(): Avoid false positive security warning
Date: Tue, 01 Nov 2011 21:30:12 +0100	[thread overview]
Message-ID: <4EB056D4.1040402@redhat.com> (raw)
In-Reply-To: <1320160175-18976-20-git-send-email-Jes.Sorensen@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 710 bytes --]

On 11/01/11 16:09, Jes.Sorensen@redhat.com wrote:
> From: Jes Sorensen <Jes.Sorensen@redhat.com>
> 
> S_ISBLK() and S_ISLNK() are mutually exclusive. By swapping the checks
> round and testing S_ISBLK() first, we avoid having to silence the
> compiler for uninitialized variable usage, and avoid a warning from
> security checking tools.
> 
> Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>

Looking at this one again, I realized it doesn't solve the real problem.
In particular if /dev/md64 already exists as a symlink and /dev/md64p1
exists as a device node, we end up comparing against random stack data.

Please discard the previous patch and use this one instead.

Sorry for the noise.

Cheers,
Jes

[-- Attachment #2: 0019-make_parts-Fix-case-of-comparing-against-uninitializ.patch --]
[-- Type: text/x-patch, Size: 2248 bytes --]

From a6411e70a8074d13f9ad0af6d68a6d8bb550c317 Mon Sep 17 00:00:00 2001
From: Jes Sorensen <Jes.Sorensen@redhat.com>
Date: Tue, 1 Nov 2011 14:53:37 +0100
Subject: [PATCH 19/19] make_parts(): Fix case of comparing against
 uninitialized variables

Silencing gcc's warning of uninitialized variables was hiding a bug
where if we have /dev/md64 as a symlink, and /dev/md64p1 was a real
device node.

In this case major_num and minor_num would not get populated, but we
end up comparing against them because the stat for md64p1 succeeds.

Instead of using the int foo = foo trick, change the code to set
set the variables to invalid values so comparisons will fail.

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 mdopen.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/mdopen.c b/mdopen.c
index 555ab84..eac1c1f 100644
--- a/mdopen.c
+++ b/mdopen.c
@@ -38,9 +38,9 @@ void make_parts(char *dev, int cnt)
 	 * else that of dev
 	 */
 	struct stat stb;
-	int major_num = major_num; /* quiet gcc -Os unitialized warning */
-	int minor_num = minor_num; /* quiet gcc -Os unitialized warning */
-	int odig = odig; /* quiet gcc -Os unitialized warning */
+	int major_num;
+	int minor_num;
+	int odig;
 	int i;
 	int nlen = strlen(dev) + 20;
 	char *name;
@@ -53,23 +53,26 @@ void make_parts(char *dev, int cnt)
 	if (lstat(dev, &stb)!= 0)
 		return;
 
-	if (S_ISLNK(stb.st_mode)) {
+	if (S_ISBLK(stb.st_mode)) {
+		major_num = major(stb.st_rdev);
+		minor_num = minor(stb.st_rdev);
+		odig = -1;
+	} else if (S_ISLNK(stb.st_mode)) {
 		int len = readlink(dev, orig, sizeof(orig));
 		if (len < 0 || len > 1000)
 			return;
 		orig[len] = 0;
 		odig = isdigit(orig[len-1]);
-	} else if (S_ISBLK(stb.st_mode)) {
-		major_num = major(stb.st_rdev);
-		minor_num = minor(stb.st_rdev);
+		major_num = -1;
+		minor_num = -1;
 	} else
-		   return;
+		return;
 	name = malloc(nlen);
 	for (i=1; i <= cnt ; i++) {
 		struct stat stb2;
 		snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i);
 		if (stat(name, &stb2)==0) {
-			if (!S_ISBLK(stb2.st_mode))
+			if (!S_ISBLK(stb2.st_mode) || !S_ISBLK(stb.st_mode))
 				continue;
 			if (stb2.st_rdev == makedev(major_num, minor_num+i))
 				continue;
-- 
1.7.6.4


  reply	other threads:[~2011-11-01 20:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-01 15:09 [PATCH 00/19] More fixes for resource leaks and warnings Jes.Sorensen
2011-11-01 15:09 ` [PATCH 01/19] Grow_Add_device(): dev_open() return a negative fd on error Jes.Sorensen
2011-11-01 15:09 ` [PATCH 02/19] Grow_addbitmap(): don't try to close a file descriptor which failed to open Jes.Sorensen
2011-11-01 15:09 ` [PATCH 03/19] Incremental(): Check return value of dev_open() before trying to use it Jes.Sorensen
2011-11-01 15:09 ` [PATCH 04/19] sysfs_unique_holder(): Check read() return value before using as buffer index Jes.Sorensen
2011-11-01 15:09 ` [PATCH 05/19] remove_devices(): readlink returns -1 on error Jes.Sorensen
2011-11-01 15:09 ` [PATCH 06/19] assemble_container_content(): fix memory leak Jes.Sorensen
2011-11-01 15:09 ` [PATCH 07/19] Grow_restart(): free() offsets after use Jes.Sorensen
2011-11-01 15:09 ` [PATCH 08/19] Assemble(): don't dup_super() before we need it Jes.Sorensen
2011-11-01 15:09 ` [PATCH 09/19] Detail(): Remember to free 'avail' Jes.Sorensen
2011-11-01 15:09 ` [PATCH 10/19] Grow_reshape(): Fix another 'sra' leak Jes.Sorensen
2011-11-01 15:09 ` [PATCH 11/19] enough_fd(): remember to free buffer for avail array Jes.Sorensen
2011-11-01 15:09 ` [PATCH 12/19] Manage_subdevs(): avoid leaking super Jes.Sorensen
2011-11-01 15:09 ` [PATCH 13/19] IncrementalScan(): Fix memory leak Jes.Sorensen
2011-11-01 15:09 ` [PATCH 14/19] Managa_ro(): free() mdi before exiting Jes.Sorensen
2011-11-01 15:09 ` [PATCH 15/19] Manage_runstop(): Avoid memory leak Jes.Sorensen
2011-11-01 15:09 ` [PATCH 16/19] Monitor(): free allocated memory on exit Jes.Sorensen
2011-11-01 15:09 ` [PATCH 17/19] bitmap_fd_read(): fix memory leak Jes.Sorensen
2011-11-01 15:09 ` [PATCH 18/19] validate_geometry_imsm_volume(): Avoid NULL pointer dereference Jes.Sorensen
2011-11-01 15:09 ` [PATCH 19/19] make_parts(): Avoid false positive security warning Jes.Sorensen
2011-11-01 20:30   ` Jes Sorensen [this message]
2011-11-02  0:25 ` [PATCH 00/19] More fixes for resource leaks and warnings NeilBrown
2011-11-02 14:39   ` Jes Sorensen
2011-11-02 21:58     ` Doug Ledford

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4EB056D4.1040402@redhat.com \
    --to=jes.sorensen@redhat.com \
    --cc=dledford@redhat.com \
    --cc=linux-raid@vger.kernel.org \
    --cc=neilb@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).