Skip to contentSkip to conversation

libselinux:add check for malloc

PATCH In review Jie Lu 2 participants 3 messages Nov 29, 2022 – Dec 16, 2022 SELinux security module

lore
Share & tools
lore

Jie Lu

author
#0 lore raw

PATCH 1/1 libselinux:add check for malloc

Add return check for regex_data_create() to avoid NULL reference of regex_data

(gdb) bt

 #0  0x00007fbde5caec14 in pthread_mutex_init () from /usr/lib64/libc.so.6
 #1  0x00007fbde5e3a489 in regex_data_create () at regex.c:260
 #2  0x00007fbde5e3a4af in regex_prepare_data (regex=regex@entry=0x7fbde4613770, pattern_string=pattern_string@entry=0x563c6799a820 "^/home$", errordata=errordata@entry=0x7ffeb83fa950) at regex.c:76
 #3  0x00007fbde5e32fe6 in compile_regex (errbuf=0x0, spec=0x7fbde4613748) at label_file.h:407
 #4  lookup_all (key=0x563c679974e5 "/var/log/kadmind.log", type=<optimized out>, partial=partial@entry=false, match_count=match_count@entry=0x0, rec=<optimized out>, rec=<optimized out>)
     at label_file.c:949

#5 0x00007fbde5e33350 in lookup (rec=<optimized out>, key=<optimized out>, type=<optimized out>) at label_file.c:1092 #6 0x00007fbde5e31878 in selabel_lookup_common (rec=0x563c67998cc0, translating=1, key=<optimized out>, type=<optimized out>) at label.c:167

---
 libselinux/src/regex.c | 3 +++
 1 file changed, 3 insertions(+)
libselinux/src/regex.c +3 -0
index 73987d9f..149a7973 100644
@@ -257,6 +257,9 @@ struct regex_data *regex_data_create(void)
 {
 	struct regex_data *regex_data =
 		(struct regex_data *)calloc(1, sizeof(struct regex_data));
+	if (!regex_data)
+		return NULL;
+
 	__pthread_mutex_init(&regex_data->match_mutex, NULL);
 	return regex_data;
 }
Signature · 1 line
2.27.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Jie Lu

Add return check for regex_data_create() to avoid NULL reference of regex_data

(gdb) bt

 #0  0x00007fbde5caec14 in pthread_mutex_init () from /usr/lib64/libc.so.6
 #1  0x00007fbde5e3a489 in regex_data_create () at regex.c:260
 #2  0x00007fbde5e3a4af in regex_prepare_data (regex=regex@entry=0x7fbde4613770, pattern_string=pattern_string@entry=0x563c6799a820 "^/home$", errordata=errordata@entry=0x7ffeb83fa950) at regex.c:76
 #3  0x00007fbde5e32fe6 in compile_regex (errbuf=0x0, spec=0x7fbde4613748) at label_file.h:407
 #4  lookup_all (key=0x563c679974e5 "/var/log/kadmind.log", type=<optimized out>, partial=partial@entry=false, match_count=match_count@entry=0x0, rec=<optimized out>, rec=<optimized out>)
     at label_file.c:949
Quoted 4 lines

#5 0x00007fbde5e33350 in lookup (rec=<optimized out>, key=<optimized out>, type=<optimized out>) at label_file.c:1092 #6 0x00007fbde5e31878 in selabel_lookup_common (rec=0x563c67998cc0, translating=1, key=<optimized out>, type=<optimized out>) at label.c:167

Quoted 21 lines
---
 libselinux/src/regex.c | 3 +++
 1 file changed, 3 insertions(+)
libselinux/src/regex.c +3 -1
index 73987d9f..149a7973 100644
@@ -257,6 +257,9 @@ struct regex_data *regex_data_create(void)
 {
        struct regex_data *regex_data =
                (struct regex_data *)calloc(1, sizeof(struct regex_data));
+       if (!regex_data)
+               return NULL;
+
        __pthread_mutex_init(&regex_data->match_mutex, NULL);
        return regex_data;
 }
--

2.27.0

#2 lore raw
Quoted 15 lines · James Carter
Quoted 13 lines · Jie Lu

Add return check for regex_data_create() to avoid NULL reference of regex_data

(gdb) bt

 #0  0x00007fbde5caec14 in pthread_mutex_init () from /usr/lib64/libc.so.6
 #1  0x00007fbde5e3a489 in regex_data_create () at regex.c:260
 #2  0x00007fbde5e3a4af in regex_prepare_data (regex=regex@entry=0x7fbde4613770, pattern_string=pattern_string@entry=0x563c6799a820 "^/home$", errordata=errordata@entry=0x7ffeb83fa950) at regex.c:76
 #3  0x00007fbde5e32fe6 in compile_regex (errbuf=0x0, spec=0x7fbde4613748) at label_file.h:407
 #4  lookup_all (key=0x563c679974e5 "/var/log/kadmind.log", type=<optimized out>, partial=partial@entry=false, match_count=match_count@entry=0x0, rec=<optimized out>, rec=<optimized out>)
     at label_file.c:949

#5 0x00007fbde5e33350 in lookup (rec=<optimized out>, key=<optimized out>, type=<optimized out>) at label_file.c:1092 #6 0x00007fbde5e31878 in selabel_lookup_common (rec=0x563c67998cc0, translating=1, key=<optimized out>, type=<optimized out>) at label.c:167

Quoted 4 lines
Quoted 1 line

Merged.
Thanks,
Jim

Quoted 21 lines
Quoted 21 lines
---
 libselinux/src/regex.c | 3 +++
 1 file changed, 3 insertions(+)
libselinux/src/regex.c +3 -1
index 73987d9f..149a7973 100644
@@ -257,6 +257,9 @@ struct regex_data *regex_data_create(void)
 {
        struct regex_data *regex_data =
                (struct regex_data *)calloc(1, sizeof(struct regex_data));
+       if (!regex_data)
+               return NULL;
+
        __pthread_mutex_init(&regex_data->match_mutex, NULL);
        return regex_data;
 }
--

2.27.0