Skip to contentSkip to conversation

libselinux: avoid out-of-bounds access on zero-length lines

PATCH In review Stephen Smalley 3 participants 5 messages Jun 10 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: avoid out-of-bounds access on zero-length lines

From: netliomax25-code <[email protected]>

Several helpers read a line with fgets()/fgets_unlocked() and then index buf[strlen(buf) - 1] to strip the trailing newline without checking the line is non-empty. A line beginning with a NUL byte is stored but yields strlen(buf) == 0, so the index becomes buf[(size_t)-1]. Guard the index with a non-empty check, matching the form already used in get_ordered_context_list().

---
 libselinux/src/get_default_type.c     | 2 +-
 libselinux/src/is_customizable_type.c | 3 ++-
 libselinux/src/matchmediacon.c        | 2 +-
 libselinux/src/query_user_context.c   | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
libselinux/src/get_default_type.c +1 -1
index e2186a19..8955c505 100644
@@ -38,7 +38,7 @@ static int find_default_type(FILE *fp, const char *role, char **type)
 			errno = EINVAL;
 			return -1;
 		}
-		if (buf[strlen(buf) - 1])
+		if (buf[0] && buf[strlen(buf) - 1])
 			buf[strlen(buf) - 1] = 0;
 
 		ptr = buf;
libselinux/src/is_customizable_type.c +2 -1
index 0aa790e3..992747fb 100644
@@ -44,7 +44,8 @@ static void customizable_init(void)
 			i = 0;
 			while (fgets_unlocked(buf, selinux_page_size, fp) &&
 			       i < ctr) {
-				buf[strlen(buf) - 1] = 0;
+				if (buf[0])
+					buf[strlen(buf) - 1] = 0;
 				list[i] = strdup(buf);
 				if (!list[i]) {
 					unsigned int j;
libselinux/src/matchmediacon.c +1 -1
index 3e7544b7..ca3d7980 100644
@@ -26,7 +26,7 @@ int matchmediacon(const char *media, char **con)
 			fclose(infile);
 			return -1;
 		}
-		if (current_line[strlen(current_line) - 1])
+		if (current_line[0] && current_line[strlen(current_line) - 1])
 			current_line[strlen(current_line) - 1] = 0;
 		/* Skip leading whitespace before the partial context. */
 		ptr = current_line;
libselinux/src/query_user_context.c +1 -1
index 0a5e8315..c2196de2 100644
@@ -85,7 +85,7 @@ static void get_field(const char *fieldstr, char *newfield, int newfieldlen)
 		if (fgets(newfield, newfieldlen, stdin) == NULL)
 			continue;
 		fflush(stdin);
-		if (newfield[strlen(newfield) - 1] == '\n')
+		if (newfield[0] && newfield[strlen(newfield) - 1] == '\n')
 			newfield[strlen(newfield) - 1] = '\0';
 
 		if (strlen(newfield) == 0) {
Signature · 1 line
2.54.0
#1 lore raw

libselinux/src/get_default_type.c static int find_default_type(FILE *fp, const char *role, char **type) · line 42

Quoted 3 lines · Stephen Smalley
libselinux/src/get_default_type.c +1 -1
-		if (buf[strlen(buf) - 1])
+		if (buf[0] && buf[strlen(buf) - 1])
 			buf[strlen(buf) - 1] = 0;

While making these changes, I would suggest always using '\0' instead of plain 0.

br,
KK

#2 lore raw
Quoted 7 lines · Kalevi Kolttonen

libselinux/src/get_default_type.c static int find_default_type(FILE *fp, const char *role, char **type) · line 42

Quoted 3 lines · Stephen Smalley
libselinux/src/get_default_type.c +1 -1
-		if (buf[strlen(buf) - 1])
+		if (buf[0] && buf[strlen(buf) - 1])
 			buf[strlen(buf) - 1] = 0;

While making these changes, I would suggest always using '\0' instead of plain 0.

Never mind, just merge that. I will send a trivial patch later for libselinux.

br,
KK

Acked-by on v1 1/1
#3 lore raw
In reply to Stephen Smalley
Quoted 11 lines · Stephen Smalley

From: netliomax25-code <[email protected]>

Several helpers read a line with fgets()/fgets_unlocked() and then index buf[strlen(buf) - 1] to strip the trailing newline without checking the line is non-empty. A line beginning with a NUL byte is stored but yields strlen(buf) == 0, so the index becomes buf[(size_t)-1]. Guard the index with a non-empty check, matching the form already used in get_ordered_context_list().

Quoted 63 lines
---
 libselinux/src/get_default_type.c     | 2 +-
 libselinux/src/is_customizable_type.c | 3 ++-
 libselinux/src/matchmediacon.c        | 2 +-
 libselinux/src/query_user_context.c   | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
libselinux/src/get_default_type.c +1 -1
index e2186a19..8955c505 100644
@@ -38,7 +38,7 @@ static int find_default_type(FILE *fp, const char *role, char **type)
                        errno = EINVAL;
                        return -1;
                }
-               if (buf[strlen(buf) - 1])
+               if (buf[0] && buf[strlen(buf) - 1])
                        buf[strlen(buf) - 1] = 0;
 
                ptr = buf;
libselinux/src/is_customizable_type.c +2 -1
index 0aa790e3..992747fb 100644
@@ -44,7 +44,8 @@ static void customizable_init(void)
                        i = 0;
                        while (fgets_unlocked(buf, selinux_page_size, fp) &&
                               i < ctr) {
-                               buf[strlen(buf) - 1] = 0;
+                               if (buf[0])
+                                       buf[strlen(buf) - 1] = 0;
                                list[i] = strdup(buf);
                                if (!list[i]) {
                                        unsigned int j;
libselinux/src/matchmediacon.c +1 -1
index 3e7544b7..ca3d7980 100644
@@ -26,7 +26,7 @@ int matchmediacon(const char *media, char **con)
                        fclose(infile);
                        return -1;
                }
-               if (current_line[strlen(current_line) - 1])
+               if (current_line[0] && current_line[strlen(current_line) - 1])
                        current_line[strlen(current_line) - 1] = 0;
                /* Skip leading whitespace before the partial context. */
                ptr = current_line;
libselinux/src/query_user_context.c +1 -2
index 0a5e8315..c2196de2 100644
@@ -85,7 +85,7 @@ static void get_field(const char *fieldstr, char *newfield, int newfieldlen)
                if (fgets(newfield, newfieldlen, stdin) == NULL)
                        continue;
                fflush(stdin);
-               if (newfield[strlen(newfield) - 1] == '\n')
+               if (newfield[0] && newfield[strlen(newfield) - 1] == '\n')
                        newfield[strlen(newfield) - 1] = '\0';
 
                if (strlen(newfield) == 0) {
--

2.54.0

#4 lore raw
Quoted 13 lines · James Carter
Quoted 10 lines · Stephen Smalley

From: netliomax25-code <[email protected]>

Several helpers read a line with fgets()/fgets_unlocked() and then index buf[strlen(buf) - 1] to strip the trailing newline without checking the line is non-empty. A line beginning with a NUL byte is stored but yields strlen(buf) == 0, so the index becomes buf[(size_t)-1]. Guard the index with a non-empty check, matching the form already used in get_ordered_context_list().

Quoted 4 lines
Quoted 1 line

Merged.
Thanks,
Jim

Quoted 63 lines
Quoted 63 lines
---
 libselinux/src/get_default_type.c     | 2 +-
 libselinux/src/is_customizable_type.c | 3 ++-
 libselinux/src/matchmediacon.c        | 2 +-
 libselinux/src/query_user_context.c   | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
libselinux/src/get_default_type.c +1 -1
index e2186a19..8955c505 100644
@@ -38,7 +38,7 @@ static int find_default_type(FILE *fp, const char *role, char **type)
                        errno = EINVAL;
                        return -1;
                }
-               if (buf[strlen(buf) - 1])
+               if (buf[0] && buf[strlen(buf) - 1])
                        buf[strlen(buf) - 1] = 0;
 
                ptr = buf;
libselinux/src/is_customizable_type.c +2 -1
index 0aa790e3..992747fb 100644
@@ -44,7 +44,8 @@ static void customizable_init(void)
                        i = 0;
                        while (fgets_unlocked(buf, selinux_page_size, fp) &&
                               i < ctr) {
-                               buf[strlen(buf) - 1] = 0;
+                               if (buf[0])
+                                       buf[strlen(buf) - 1] = 0;
                                list[i] = strdup(buf);
                                if (!list[i]) {
                                        unsigned int j;
libselinux/src/matchmediacon.c +1 -1
index 3e7544b7..ca3d7980 100644
@@ -26,7 +26,7 @@ int matchmediacon(const char *media, char **con)
                        fclose(infile);
                        return -1;
                }
-               if (current_line[strlen(current_line) - 1])
+               if (current_line[0] && current_line[strlen(current_line) - 1])
                        current_line[strlen(current_line) - 1] = 0;
                /* Skip leading whitespace before the partial context. */
                ptr = current_line;
libselinux/src/query_user_context.c +1 -2
index 0a5e8315..c2196de2 100644
@@ -85,7 +85,7 @@ static void get_field(const char *fieldstr, char *newfield, int newfieldlen)
                if (fgets(newfield, newfieldlen, stdin) == NULL)
                        continue;
                fflush(stdin);
-               if (newfield[strlen(newfield) - 1] == '\n')
+               if (newfield[0] && newfield[strlen(newfield) - 1] == '\n')
                        newfield[strlen(newfield) - 1] = '\0';
 
                if (strlen(newfield) == 0) {
--

2.54.0