Skip to contentSkip to conversation

libselinux: 1/2 Introduce {get,set}procattrcon

PATCH In review Eric Paris 2 participants 2 messages Jun 23, 2006 – Jun 24, 2006 SELinux security module

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 libselinux: 1/2 Introduce {get,set}procattrcon

The below attached patch implements two new function, get and set procattrcon. These are generic functions that take a context and a path and attempt to write that context to that path. These are used by of exported libselinux interfaces, like getfscreatecon, as a generic backend to reduce duplication of code.

 include/selinux/selinux.h |    8 +++++
 src/getprocattrcon.c      |   67 ++++++++++++++++++++++++++++++++++++++++++++++
 src/selinux_internal.h    |    4 ++
 src/setprocattrcon.c      |   40 +++++++++++++++++++++++++++
src/setprocattrcon.c renamed from src/setprocattrcon.c.p1 +40 -0
@@ -0,0 +1,40 @@
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include "selinux_internal.h"
+
+int setprocattrcon_raw(char *context, char *proc_entry)
+{
+	int fd;
+	ssize_t ret;
+
+	fd = open(proc_entry, 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;
+}
+hidden_def(setprocattrcon_raw)
+
+int setprocattrcon(char *context, char *proc_entry)
+{
+	int ret;
+	security_context_t rcontext = context;
+
+	if (selinux_trans_to_raw_context(context, &rcontext))
+		return -1;
+
+ 	ret = setprocattrcon_raw(rcontext, proc_entry);
+
+	freecon(rcontext);
+
+	return ret;
+}
+hidden_def(setprocattrcon)
src/selinux_internal.h renamed from src/selinux_internal.h.p1 +4 -0
@@ -26,6 +26,10 @@ hidden_proto(is_selinux_enabled)
 hidden_proto(is_selinux_mls_enabled)
 hidden_proto(freecon)
 hidden_proto(freeconary)
+hidden_proto(getprocattrcon)
+hidden_proto(getprocattrcon_raw)
+hidden_proto(setprocattrcon)
+hidden_proto(setprocattrcon_raw)
 hidden_proto(getprevcon)
 hidden_proto(getprevcon_raw)
 hidden_proto(getcon)
src/getprocattrcon.c renamed from src/getprocattrcon.c.p1 +67 -0

67 lines not on this page · raw message

include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +8 -0
@@ -24,6 +24,14 @@ extern void freeconary(security_context_
 
 /* Wrappers for the /proc/pid/attr API. */
 
+/* Generic /proc pid attr handlers.  These will either get or set the context
+   from or into the proc location passed to them.  Should only be called from 
+   inside libselinux */
+extern int getprocattrcon(security_context_t *con, char *path);
+extern int getprocattrcon_raw(security_context_t *con, char *path);
+extern int setprocattrcon(security_context_t con, char *path);
+extern int setprocattrcon_raw(security_context_t con, char *path);
+
 /* Get current context, and set *con to refer to it.
    Caller must free via freecon. */
 extern int getcon(security_context_t *con);

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

#1 lore raw
Quoted 17 lines · Eric Paris

The below attached patch implements two new function, get and set procattrcon. These are generic functions that take a context and a path and attempt to write that context to that path. These are used by of exported libselinux interfaces, like getfscreatecon, as a generic backend to reduce duplication of code.

src/setprocattrcon.c renamed from src/setprocattrcon.c.p1 +8 -0
@@ -0,0 +1,40 @@
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include "selinux_internal.h"
+
+int setprocattrcon_raw(char *context, char *proc_entry)
+{
+	int fd;

src/setprocattrcon.c line 18

Quoted 10 lines
src/setprocattrcon.c renamed from src/setprocattrcon.c.p1 +10 -0
+	ssize_t ret;
+
+	fd = open(proc_entry, 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);

Could you save and restore errno around the close? I know it didn't do
that before, but still...

Quoted 6 lines
src/setprocattrcon.c renamed from src/setprocattrcon.c.p1 +6 -0
+	if (ret < 0)
+		return -1;
+	else
+		return 0;
+}
+hidden_def(setprocattrcon_raw)

[...]

Quoted 1 line

+hidden_def(setprocattrcon)

These make it impossible to call those functions from outside the
shared library, but you've declared them in the external header.

Quoted 17 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +8 -0
@@ -24,6 +24,14 @@ extern void freeconary(security_context_
 
 /* Wrappers for the /proc/pid/attr API. */
 
+/* Generic /proc pid attr handlers.  These will either get or set the context
+   from or into the proc location passed to them.  Should only be called from 
+   inside libselinux */
+extern int getprocattrcon(security_context_t *con, char *path);
+extern int getprocattrcon_raw(security_context_t *con, char *path);
+extern int setprocattrcon(security_context_t con, char *path);
+extern int setprocattrcon_raw(security_context_t con, char *path);
+
 /* Get current context, and set *con to refer to it.
    Caller must free via freecon. */
 extern int getcon(security_context_t *con);
Signature · 1 line
James Antill <[email protected]>