All of lore.kernel.org
 help / color / mirror / Atom feed
From: NeilBrown <neilb@suse.de>
To: dm-devel@redhat.com
Subject: [PATCH libdmraid-events 2/3] Fix some issues with use of snprintf
Date: Fri, 11 Dec 2009 14:22:57 +1100	[thread overview]
Message-ID: <20091211032408.285980198@suse.de> (raw)
In-Reply-To: 20091211032255.766079509@suse.de

[-- Attachment #1: snprint.fixes --]
[-- Type: text/plain, Size: 2140 bytes --]

Several times, the buf len argument passed to snprintf is a
'sizeof' something which is only vaguely related to the size of the buffer,
and in some cases is definitely larger than the buffer.
Also snprintf does not guarantee to produce a nul terminated string if
an overflow occurs.

So pass more appropriate buffer lengths and ensure result is nul
terminated.
Also make 'dm_num' large enough to hold more than a single digit.

Signed-off-by: NeilBrown <neilb@suse.de>
---
 libdmraid-events.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

--- libdmraid-events.orig/libdmraid-events.c
+++ libdmraid-events/libdmraid-events.c
@@ -303,6 +303,7 @@ static char *_match_port(const char *vol
 			for(i = 0; i < curr->num_sata_drives; i++) 
 				if(curr->raid_mem[i].port_num>=0){
 				snprintf(port, 4, "%d", curr->raid_mem[i].port_num);
+				port[4] = 0;
 				memcpy(buf+strlen(buf), curr->raid_mem[i].dev_name, strlen(curr->raid_mem[i].dev_name));
 				memcpy(buf+strlen(buf), "=", 1);
 				memcpy(buf+strlen(buf), port, strlen(port));
@@ -378,7 +379,7 @@ static char *_get_dev_names(const char *
 	int num = 0;
 	FILE *fd;
 	char sys_path[BUF_SIZE];
-	char dm_num[2];
+	char dm_num[5];
 	char dm_mm[MAJOR_MINOR];
 	char f_mm[MAJOR_MINOR];
 	struct dm_task *dmt;
@@ -406,8 +407,10 @@ static char *_get_dev_names(const char *
 	memset(sys_path, 0, BUF_SIZE);
 	memcpy(sys_path, SYS_DM_PATH, strlen(SYS_DM_PATH));
 	memset(dm_mm, 0, MAJOR_MINOR);
-	snprintf(dm_mm, sizeof(info.major)+sizeof(info.minor), "%d:%d", info.major, info.minor);
-	snprintf(dm_num, sizeof(num = 0), "%d", num);
+	snprintf(dm_mm, MAJOR_MINOR-1, "%d:%d", info.major, info.minor);
+	num = 0;
+	snprintf(dm_num, sizeof(dm_num), "%d", num);
+
 			
 	while(!access(strncat(sys_path, dm_num, strlen(dm_num)), F_OK)) {
 				
@@ -423,7 +426,8 @@ static char *_get_dev_names(const char *
 			/* Reset string for next iteration */
 			memset(sys_path+strlen(SYS_DM_PATH), 0, 1);
 			num++;
-			snprintf(dm_num, sizeof(num), "%d", num);					
+			snprintf(dm_num, sizeof(dm_num), "%d", num);
+			dm_num[sizeof(dm_num)-1] = 0;
 			fclose(fd);
 			continue;
 		}

  parent reply	other threads:[~2009-12-11  3:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-11  3:22 [PATCH libdmraid-events 0/3] Assorted fixes NeilBrown
2009-12-11  3:22 ` [PATCH libdmraid-events 1/3] Add some missing closedir calls NeilBrown
2009-12-11  3:22 ` NeilBrown [this message]
2009-12-11  3:22 ` [PATCH libdmraid-events 3/3] Fix minor memory leak in lib_main NeilBrown
2009-12-14 14:28 ` [PATCH libdmraid-events 0/3] Assorted fixes Heinz Mauelshagen
2009-12-15  0:43   ` Neil Brown
2009-12-15  1:07   ` Neil Brown

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=20091211032408.285980198@suse.de \
    --to=neilb@suse.de \
    --cc=dm-devel@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.