Skip to contentSkip to conversation

libselinux: 2/2 Convert functions to use new get,set procattrcon

PATCH New Eric Paris 1 participant 1 message Jun 26, 2006 SELinux security module

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 libselinux: 2/2 Convert functions to use new get,set procattrcon

The below patch will convert

-{get,set}fscreatecon()
-{get,set}execcon()
-{get,set}con()
-getpidcon()
-getprevcon()

to use the new {get,set}procattrcon() generic functions.

setcon() had a special check if the context to be written was null and returned -1 since you are not supposed to be able to set your own context to null. My testing showed that the special case was not needed as the null context would be rejected anyway with -1;

-Eric

 getcon.c         |   56 +++--------------------------------------------------
 getexeccon.c     |   58 +++----------------------------------------------------
 getfscreatecon.c |   58 +++----------------------------------------------------
 getpidcon.c      |   58 ++++++-------------------------------------------------
 getprevcon.c     |   53 +++-----------------------------------------------
 setcon.c         |   33 +++----------------------------
 setexeccon.c     |   33 +++----------------------------
 setfscreatecon.c |   35 ++++-----------------------------
 8 files changed, 36 insertions(+), 348 deletions(-)
src/getfscreatecon.c renamed from src/getfscreatecon.c.p2 +4 -54
@@ -1,64 +1,14 @@
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
 #include "selinux_internal.h"
-#include "policy.h"
+
+#define FSCREATE_PROC_ENTRY "/proc/self/attr/fscreate"
 
 int getfscreatecon_raw(security_context_t *context)
 {
-	char *buf;
-	size_t size;
-	int fd;
-	ssize_t ret;
-
-	fd = open("/proc/self/attr/fscreate", O_RDONLY);
-	if (fd < 0)
-		return -1;
-
-	size = selinux_page_size;
-	buf = malloc(size);
-	if (!buf) {
-		ret = -1;
-		goto out;
-	}
-	memset(buf, 0, size);
-
-	ret = read(fd, buf, size-1);
-	if (ret < 0)
-		goto out2;
-
-	if (ret == 0) {
-		*context = NULL;
-		goto out2;
-	}
-
-	*context = strdup(buf);
-	if (!(*context)) {
-		ret = -1;
-		goto out2;
-	}
-	ret = 0;
-out2:			
-	free(buf);
-out:
-	close(fd);
-	return ret;
+	return getprocattrcon_raw(context, FSCREATE_PROC_ENTRY);
 }
 hidden_def(getfscreatecon_raw)
 
 int getfscreatecon(security_context_t *context)
 {
-	int ret;
-	security_context_t rcontext;
-
- 	ret = getfscreatecon_raw(&rcontext);
-
-	if (!ret) {
-		ret = selinux_raw_to_trans_context(rcontext, context);
-		freecon(rcontext);
-	} 
-
-	return ret;
+	return getprocattrcon(context, FSCREATE_PROC_ENTRY);
 }
src/getexeccon.c renamed from src/getexeccon.c.p2 +4 -54

68 lines not on this page · raw message

src/setfscreatecon.c renamed from src/setfscreatecon.c.p2 +5 -30
@@ -1,39 +1,14 @@
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
 #include "selinux_internal.h"
 
-int setfscreatecon_raw(char *context)
-{
-	int fd;
-	ssize_t ret;
+#define FSCREATE_PROC_ENTRY "/proc/self/attr/fscreate"
 
-	fd = open("/proc/self/attr/fscreate", O_RDWR);
-	if (fd < 0)
-		return -1;
-	if (context) 
-		ret = write(fd, context, strlen(context)+1);
-	else
-		ret = write(fd, NULL, 0); /* clear */
-	close(fd);
-	if (ret < 0)
-		return -1;
-	else
-		return 0;
+int setfscreatecon_raw(security_context_t context)
+{
+	return setprocattrcon_raw(context, FSCREATE_PROC_ENTRY);
 }
 hidden_def(setfscreatecon_raw)
 
 int setfscreatecon(char *context)
 {
-	int ret;
-	security_context_t rcontext = context;
-
-	if (selinux_trans_to_raw_context(context, &rcontext))
-		return -1;
-
- 	ret = setfscreatecon_raw(rcontext);
-
-	freecon(rcontext);
-
-	return ret;
+ 	return setprocattrcon(context, FSCREATE_PROC_ENTRY);
 }
src/getpidcon.c renamed from src/getpidcon.c.p2 +7 -51

70 lines not on this page · raw message

src/getcon.c renamed from src/getcon.c.p2 +4 -52

67 lines not on this page · raw message

src/setexeccon.c renamed from src/setexeccon.c.p2 +4 -29
@@ -1,40 +1,15 @@
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
 #include "selinux_internal.h"
 
+#define EXEC_PROC_ENTRY "/proc/self/attr/exec"
+
 int setexeccon_raw(security_context_t context)
 {
-	int fd;
-	ssize_t ret;
-
-	fd = open("/proc/self/attr/exec", O_RDWR);
-	if (fd < 0)
-		return -1;
-	if (context) 
-		ret = write(fd, context, strlen(context)+1);
-	else
-		ret = write(fd, NULL, 0); /* clear */
-	close(fd);
-	if (ret < 0)
-		return -1;
-	else
-		return 0;
+	return setprocattrcon_raw(context, EXEC_PROC_ENTRY);
 }
 hidden_def(setexeccon_raw)
 
 int setexeccon(char *context)
 {
-	int ret;
-	security_context_t rcontext = context;
-
-	if (selinux_trans_to_raw_context(context, &rcontext))
-		return -1;
-
- 	ret = setexeccon_raw(rcontext);
-
-	freecon(rcontext);
-
-	return ret;
+ 	return setprocattrcon(context, EXEC_PROC_ENTRY);
 }
 hidden_def(setexeccon)
src/setcon.c renamed from src/setcon.c.p2 +4 -29
@@ -2,42 +2,17 @@
  * Author: Trusted Computer Solutions, Inc. <[email protected]>
  */
 
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
 #include "selinux_internal.h"
 
+#define CURRENT_PROC_ENTRY "/proc/self/attr/current"
+
 int setcon_raw(security_context_t context)
 {
-	int fd;
-	ssize_t ret;
-
-	fd = open("/proc/self/attr/current", O_RDWR);
-	if (fd < 0)
-		return -1;
-	if (context) 
-		ret = write(fd, context, strlen(context)+1);
-	else
-		ret = -1; /* we can not clear this one */
-	close(fd);
-	if (ret < 0)
-		return -1;
-	else
-		return 0;
+	return setprocattrcon_raw(context, CURRENT_PROC_ENTRY);
 }
 hidden_def(setcon_raw)
 
 int setcon(char *context)
 {
-	int ret;
-	security_context_t rcontext = context;
-
-	if (selinux_trans_to_raw_context(context, &rcontext))
-		return -1;
-
- 	ret = setcon_raw(rcontext);
-
-	freecon(rcontext);
-
-	return ret;
+	return setprocattrcon(context, CURRENT_PROC_ENTRY);
 }
src/getprevcon.c renamed from src/getprevcon.c.p2 +4 -49

64 lines not on this page · raw message

--
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.