From: Eric Paris <eparis@parisplace.org>
To: selinux@tycho.nsa.gov
Subject: [PATCH] libselinux: 1/2 Introduce {get,set}procattrcon
Date: Mon, 26 Jun 2006 12:48:55 -0400 [thread overview]
Message-ID: <1151340535.21012.150.camel@localhost.localdomain> (raw)
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.
next reply other threads:[~2006-06-26 16:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-06-26 16:48 Eric Paris [this message]
2006-06-26 18:31 ` [PATCH] libselinux: 1/2 Introduce {get,set}procattrcon Stephen Smalley
2006-06-26 19:38 ` Eric Paris
2006-06-26 20:15 ` Stephen Smalley
2006-06-27 20:30 ` Stephen Smalley
-- strict thread matches above, loose matches on Subject: below --
2006-06-23 20:32 Eric Paris
2006-06-24 1:00 ` James Antill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1151340535.21012.150.camel@localhost.localdomain \
--to=eparis@parisplace.org \
--cc=selinux@tycho.nsa.gov \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.