All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libselinux: 1/2 Introduce {get,set}procattrcon
@ 2006-06-23 20:32 Eric Paris
  2006-06-24  1:00 ` James Antill
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Paris @ 2006-06-23 20:32 UTC (permalink / raw)
  To: selinux

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

--- libselinux-1.30.15/src/setprocattrcon.c.p1	2006-06-23 15:05:25.000000000 -0400
+++ libselinux-1.30.15/src/setprocattrcon.c	2006-06-23 15:10:05.000000000 -0400
@@ -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)
--- libselinux-1.30.15/src/selinux_internal.h.p1	2006-06-23 15:05:25.000000000 -0400
+++ libselinux-1.30.15/src/selinux_internal.h	2006-06-23 15:20:06.000000000 -0400
@@ -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)
--- libselinux-1.30.15/src/getprocattrcon.c.p1	2006-06-23 15:05:25.000000000 -0400
+++ libselinux-1.30.15/src/getprocattrcon.c	2006-06-23 15:10:05.000000000 -0400
@@ -0,0 +1,67 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "selinux_internal.h"
+#include "policy.h"
+
+int getprocattrcon_raw(security_context_t *context, char *proc_entry)
+{
+	char *buf;
+	size_t size;
+	int fd;
+	ssize_t ret;
+
+	fd = open(proc_entry, O_RDONLY);
+	if (fd < 0)
+		return -1;
+
+	size = selinux_page_size;
+	buf = malloc(size);
+	if (!buf) {
+		ret = -1;
+		goto out;
+	}
+	memset(buf, 0, size);
+
+	do {
+		ret = read(fd, buf, size-1);
+	} while (ret < 0 && errno == EINTR);
+	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;
+}
+hidden_def(getprocattrcon_raw)
+
+int getprocattrcon(security_context_t *context, char *proc_entry)
+{
+	int ret;
+	security_context_t rcontext;
+
+ 	ret = getprocattrcon_raw(&rcontext, proc_entry);
+
+	if (!ret) {
+		ret = selinux_raw_to_trans_context(rcontext, context);
+		freecon(rcontext);
+	} 
+
+	return ret;
+}
+hidden_def(getprocattrcon)
--- libselinux-1.30.15/include/selinux/selinux.h.p1	2006-06-23 15:05:25.000000000 -0400
+++ libselinux-1.30.15/include/selinux/selinux.h	2006-06-23 15:20:06.000000000 -0400
@@ -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 majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 7+ messages in thread
* [PATCH] libselinux: 1/2 Introduce {get,set}procattrcon
@ 2006-06-26 16:48 Eric Paris
  2006-06-26 18:31 ` Stephen Smalley
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Paris @ 2006-06-26 16:48 UTC (permalink / raw)
  To: selinux

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.

-Eric

 include/selinux/selinux.h |    8 +++++
 src/getprocattrcon.c      |   70 ++++++++++++++++++++++++++++++++++++++++++++++
 src/selinux_internal.h    |    4 ++
 src/setprocattrcon.c      |   48 +++++++++++++++++++++++++++++++
 4 files changed, 130 insertions(+)

--- libselinux-1.30.15/src/setprocattrcon.c.p1	2006-06-26 12:00:54.000000000 -0400
+++ libselinux-1.30.15/src/setprocattrcon.c	2006-06-26 12:20:21.000000000 -0400
@@ -0,0 +1,48 @@
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include "selinux_internal.h"
+
+int setprocattrcon_raw(char *context, char *proc_entry)
+{
+	int fd;
+	ssize_t ret;
+	int errno_hold;
+
+	fd = open(proc_entry, O_RDWR);
+	if (fd < 0)
+		return -1;
+	if (context) 
+		do {
+			ret = write(fd, context, strlen(context)+1);
+		} while (ret < 0 && errno == EINTR);
+	else
+		do {
+			ret = write(fd, NULL, 0); /* clear */
+		} while (ret < 0 && errno == EINTR);
+	errno_hold = errno;
+	close(fd);
+	errno = errno_hold;
+	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)
--- libselinux-1.30.15/src/selinux_internal.h.p1	2006-06-16 15:08:51.000000000 -0400
+++ libselinux-1.30.15/src/selinux_internal.h	2006-06-26 12:00:54.000000000 -0400
@@ -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)
--- libselinux-1.30.15/src/getprocattrcon.c.p1	2006-06-26 12:00:54.000000000 -0400
+++ libselinux-1.30.15/src/getprocattrcon.c	2006-06-26 12:19:23.000000000 -0400
@@ -0,0 +1,70 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "selinux_internal.h"
+#include "policy.h"
+
+int getprocattrcon_raw(security_context_t *context, char *proc_entry)
+{
+	char *buf;
+	size_t size;
+	int fd;
+	ssize_t ret;
+	int errno_hold;
+
+	fd = open(proc_entry, O_RDONLY);
+	if (fd < 0)
+		return -1;
+
+	size = selinux_page_size;
+	buf = malloc(size);
+	if (!buf) {
+		ret = -1;
+		goto out;
+	}
+	memset(buf, 0, size);
+
+	do {
+		ret = read(fd, buf, size-1);
+	} while (ret < 0 && errno == EINTR);
+	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:
+	errno_hold = errno;
+	close(fd);
+	errno = errno_hold;
+	return ret;
+}
+hidden_def(getprocattrcon_raw)
+
+int getprocattrcon(security_context_t *context, char *proc_entry)
+{
+	int ret;
+	security_context_t rcontext;
+
+ 	ret = getprocattrcon_raw(&rcontext, proc_entry);
+
+	if (!ret) {
+		ret = selinux_raw_to_trans_context(rcontext, context);
+		freecon(rcontext);
+	} 
+
+	return ret;
+}
+hidden_def(getprocattrcon)
--- libselinux-1.30.15/include/selinux/selinux.h.p1	2006-06-26 12:00:54.000000000 -0400
+++ libselinux-1.30.15/include/selinux/selinux.h	2006-06-26 12:00:54.000000000 -0400
@@ -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 majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-06-27 20:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-23 20:32 [PATCH] libselinux: 1/2 Introduce {get,set}procattrcon Eric Paris
2006-06-24  1:00 ` James Antill
  -- strict thread matches above, loose matches on Subject: below --
2006-06-26 16:48 Eric Paris
2006-06-26 18:31 ` Stephen Smalley
2006-06-26 19:38   ` Eric Paris
2006-06-26 20:15     ` Stephen Smalley
2006-06-27 20:30       ` Stephen Smalley

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.