Skip to contentSkip to conversation

libselinux python binding segfault patch

PATCH Daniel J Walsh 2 participants 6 messages Jan 24, 2007 – Jan 26, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Binary files nsalibselinux/src/selinux.pyc and libselinux-1.34.0/src/selinux.pyc differ

src/selinuxswig.i +1 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig.i libselinux-1.34.0/src/selinuxswig.i
@@ -25,7 +25,7 @@
 %apply int *OUTPUT { int * };
 %apply int *OUTPUT { size_t * };
 
-%typemap(in, numinputs=0) security_context_t *(security_context_t temp) {
+%typemap(in, numinputs=0) security_context_t *(security_context_t temp=NULL) {
 	$1 = &temp;
 }
 %typemap(argout) security_context_t * (char *temp) {
src/selinuxswig_wrap.c +16 -16
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig_wrap.c libselinux-1.34.0/src/selinuxswig_wrap.c
@@ -2831,7 +2831,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -2878,7 +2878,7 @@
   int result;
   int val1 ;
   int ecode1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -2910,7 +2910,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -2936,7 +2936,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -2980,7 +2980,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -3024,7 +3024,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -3068,7 +3068,7 @@
   PyObject *resultobj = 0;
   security_context_t *arg1 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp1 ;
+  security_context_t temp1 = NULL ;
   char *temp10 ;
   
   {
@@ -3116,7 +3116,7 @@
   int res1 ;
   char *buf1 = 0 ;
   int alloc1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -3154,7 +3154,7 @@
   int res1 ;
   char *buf1 = 0 ;
   int alloc1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -3191,7 +3191,7 @@
   int result;
   int val1 ;
   int ecode1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -3313,7 +3313,7 @@
   int result;
   int val1 ;
   int ecode1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -3474,7 +3474,7 @@
   security_context_t arg1 ;
   security_context_t *arg2 = (security_context_t *) 0 ;
   int result;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -3734,7 +3734,7 @@
   int alloc1 = 0 ;
   unsigned int val2 ;
   int ecode2 = 0 ;
-  security_context_t temp3 ;
+  security_context_t temp3 = NULL ;
   char *temp30 ;
   PyObject * obj0 = 0 ;
   PyObject * obj1 = 0 ;
@@ -3778,7 +3778,7 @@
   int res1 ;
   char *buf1 = 0 ;
   int alloc1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -4212,7 +4212,7 @@
   int res1 ;
   char *buf1 = 0 ;
   int alloc1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
@@ -4250,7 +4250,7 @@
   int res1 ;
   char *buf1 = 0 ;
   int alloc1 = 0 ;
-  security_context_t temp2 ;
+  security_context_t temp2 = NULL ;
   char *temp20 ;
   PyObject * obj0 = 0 ;
   
#1 lore raw
Quoted 2 lines · Daniel J Walsh

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Is the bug actually in the python binding or just in the callers? The actual bug report on fedora-selinux-list was for chcat, and chcat wasn't checking the return code from selinux.getfilecon and just dereferencing the string.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw
Quoted 11 lines · Stephen Smalley
Quoted 2 lines · Daniel J Walsh

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Is the bug actually in the python binding or just in the callers? The actual bug report on fedora-selinux-list was for chcat, and chcat wasn't checking the return code from selinux.getfilecon and just dereferencing the string.

It is in the binding. Basically we we not setting the security context to NULL before calling setfilecon, and then checking if it was null, to figure out success. The real change is in selinuxswig.i

#python

Quoted 2 lines

import selinux
selinux.getfilecon("/notafile")

Segmentation Fault

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
Quoted 13 lines · Daniel J Walsh
Quoted 12 lines · Stephen Smalley
Quoted 3 lines · Daniel J Walsh

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Is the bug actually in the python binding or just in the callers? The actual bug report on fedora-selinux-list was for chcat, and chcat wasn't checking the return code from selinux.getfilecon and just dereferencing the string.

Quoted 8 lines

It is in the binding. Basically we we not setting the security context to NULL before calling setfilecon, and then checking if it was null, to figure out success. The real change is in selinuxswig.i

#python

Quoted 2 lines

import selinux
selinux.getfilecon("/notafile")

Segmentation Fault

In that case, I think the same issue may apply to other typemaps, e.g. in semanage.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw
Quoted 19 lines · Stephen Smalley
Quoted 17 lines · Daniel J Walsh
Quoted 13 lines · Stephen Smalley
Quoted 3 lines · Daniel J Walsh

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Is the bug actually in the python binding or just in the callers? The actual bug report on fedora-selinux-list was for chcat, and chcat wasn't checking the return code from selinux.getfilecon and just dereferencing the string.

It is in the binding. Basically we we not setting the security context to NULL before calling setfilecon, and then checking if it was null, to figure out success. The real change is in selinuxswig.i

Quoted 10 lines
Quoted 5 lines

#python

Quoted 2 lines

import selinux
selinux.getfilecon("/notafile")

Segmentation Fault

In that case, I think the same issue may apply to other typemaps, e.g. in semanage.

Better safe then sorry.

Additional initializations in libselinux and libsemanage.

Binary files nsalibselinux/src/selinux.pyc and libselinux-1.34.0/src/selinux.pyc differ

src/selinuxswig.i +3 -3
diff --exclude-from=exclude -N -u -r nsalibselinux/src/selinuxswig.i libselinux-1.34.0/src/selinuxswig.i
@@ -25,10 +25,10 @@
 %apply int *OUTPUT { int * };
 %apply int *OUTPUT { size_t * };
 
-%typemap(in, numinputs=0) security_context_t *(security_context_t temp) {
+%typemap(in, numinputs=0) security_context_t *(security_context_t temp=NULL) {
 	$1 = &temp;
 }
-%typemap(argout) security_context_t * (char *temp) {
+%typemap(argout) security_context_t * (char *temp=NULL) {
 	if (*$1)
 		temp = *$1;
 	else
@@ -126,7 +126,7 @@
 extern int selinux_raw_to_trans_context(char *raw, 
 					security_context_t *transp);
 
-%typemap(in, numinputs=0) char **(char *temp) {
+%typemap(in, numinputs=0) char **(char *temp=NULL) {
 	$1 = &temp;
 }
 
src/selinuxswig_wrap.c +35 -35

178 lines not on this page · raw message

utils/getfilecon.c +4 -1
diff --exclude-from=exclude -N -u -r nsalibselinux/utils/getfilecon.c libselinux-1.34.0/utils/getfilecon.c
@@ -5,7 +5,7 @@
 
 int main(int argc, char **argv)
 {
-	char *buf;
+	char *buf=NULL;
 	int rc, i;
 
 	if (argc < 2) {
@@ -15,6 +15,9 @@
 
 	for (i = 1; i < argc; i++) {
 		rc = getfilecon(argv[i], &buf);
+		if (buf) {
+			printf("Buf has data\n");
+		}
 		if (rc < 0) {
 			fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
 				argv[i]);
src/semanageswig_python.i +25 -25

200 lines not on this page · raw message

src/semanageswig_wrap.c +69 -69

552 lines not on this page · raw message

#5 lore raw
Quoted 29 lines · Daniel J Walsh
Quoted 28 lines · Stephen Smalley
Quoted 25 lines · Daniel J Walsh
Quoted 14 lines · Stephen Smalley
Quoted 4 lines · Daniel J Walsh

This patch fixes a segfault in the python binding when getfilecon is called with a non existant file.

Is the bug actually in the python binding or just in the callers? The actual bug report on fedora-selinux-list was for chcat, and chcat wasn't checking the return code from selinux.getfilecon and just dereferencing the string.

It is in the binding. Basically we we not setting the security context to NULL before calling setfilecon, and then checking if it was null, to figure out success. The real change is in selinuxswig.i

#python

Quoted 2 lines

import selinux
selinux.getfilecon("/notafile")

Segmentation Fault

Quoted 8 lines
Quoted 4 lines

In that case, I think the same issue may apply to other typemaps, e.g. in semanage.

Better safe then sorry.

Additional initializations in libselinux and libsemanage.

Thanks, merged.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.