Skip to contentSkip to conversation

Fix unterminated buffer after readlink() call

PATCH In review Thomas Jarosch 2 participants 2 messages Oct 13, 2011 – Oct 17, 2011 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Fix unterminated buffer after readlink() call

---
 mdopen.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
mdopen.c +5 -1
index 0a17421..555ab84 100644
@@ -360,8 +360,12 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
 
 			if (lstat(chosen, &stb) == 0) {
 				char buf[300];
+				ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1);
+				if (link_len >= 0)
+					buf[link_len] = '\0';
+
 				if ((stb.st_mode & S_IFMT) != S_IFLNK ||
-				    readlink(chosen, buf, 300) <0 ||
+				    link_len < 0 ||
 				    strcmp(buf, devname) != 0) {
 					fprintf(stderr, Name ": %s exists - ignoring\n",
 						chosen);
Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 14 lines · On Thu, 13 Oct 2011 11:22:13 +0200 Thomas Jarosch…
---
 mdopen.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
mdopen.c +0 -0
index 0a17421..555ab84 100644
@@ -360,8 +360,12 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
 
 			if (lstat(chosen, &stb) == 0) {
 				char buf[300];

mdopen.c int create_mddev(char *dev, char *name, int autof, int trustworthy, · line 371

Quoted 10 lines
mdopen.c +5 -1
+				ssize_t link_len = readlink(chosen, buf, sizeof(buf)-1);
+				if (link_len >= 0)
+					buf[link_len] = '\0';
+
 				if ((stb.st_mode & S_IFMT) != S_IFLNK ||
-				    readlink(chosen, buf, 300) <0 ||
+				    link_len < 0 ||
 				    strcmp(buf, devname) != 0) {
 					fprintf(stderr, Name ": %s exists - ignoring\n",
 						chosen);

Thanks. I've applied this and the other one.

NeilBrown