libselinux: avoid out-of-bounds access on zero-length lines
Stephen Smalley
maintainerPATCH 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
@@ -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
@@ -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
@@ -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
@@ -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
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
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
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().Signed-off-by Kartik Kenchi <[email protected]>
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@@ -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@@ -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@@ -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@@ -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
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
Signed-off-by Kartik Kenchi <[email protected]>Acked-by James Carter <[email protected]>
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@@ -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@@ -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@@ -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@@ -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