Skip to contentSkip to conversation

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

PATCH In review Eric Paris 2 participants 5 messages Jun 26, 2006 – Jun 27, 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.

-Eric

 include/selinux/selinux.h |    8 +++++
 src/getprocattrcon.c      |   70 ++++++++++++++++++++++++++++++++++++++++++++++
 src/selinux_internal.h    |    4 ++
 src/setprocattrcon.c      |   48 +++++++++++++++++++++++++++++++
 4 files changed, 130 insertions(+)
src/setprocattrcon.c renamed from src/setprocattrcon.c.p1 +48 -0
@@ -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)
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 +70 -0

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

include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +1 -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

include/selinux/selinux.h extern void freeconary(security_context_ · line 37

Quoted 10 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +7 -0
+   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);

If they should only be called from inside libselinux, why are they defined in selinux/selinux.h, and exported to users of the library?

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.
author
#2 lore raw
Quoted 6 lines · Stephen Smalley
Quoted 6 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +0 -0
@@ -24,6 +24,14 @@ extern void freeconary(security_context_
 
 /* Wrappers for the /proc/pid/attr API. */
 
Quoted 14 lines

include/selinux/selinux.h extern void freeconary(security_context_ · line 37

Quoted 11 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +8 -0
+/* 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);

If they should only be called from inside libselinux, why are they defined in selinux/selinux.h, and exported to users of the library?

When I originally typed that comment I was still under the misguided impression they were being hidden (which should have stood out as strange for the same reason it did to james). I could see value if you had done something custom to your kernel or your libselinux didn't have interfaces for all of the /proc/pid/attr/* (as is now the case since there are kernels with keycreate and sockcreate but no libselinux interface, even if that will get fixed shortly.) I don't think it's unreasonable to remove that comment and just leave them as visable functions. Do you want another patch to remove the comment? Anyone have feelings that we shouldn't export such a general interface?

-Eric

--
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 22 lines · Eric Paris
Quoted 6 lines · Stephen Smalley
Quoted 6 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +0 -0
@@ -24,6 +24,14 @@ extern void freeconary(security_context_
 
 /* Wrappers for the /proc/pid/attr API. */
 
Quoted 14 lines

include/selinux/selinux.h extern void freeconary(security_context_ · line 37

Quoted 11 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +8 -0
+/* 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);

If they should only be called from inside libselinux, why are they defined in selinux/selinux.h, and exported to users of the library?

Quoted 10 lines

When I originally typed that comment I was still under the misguided impression they were being hidden (which should have stood out as strange for the same reason it did to james). I could see value if you had done something custom to your kernel or your libselinux didn't have interfaces for all of the /proc/pid/attr/* (as is now the case since there are kernels with keycreate and sockcreate but no libselinux interface, even if that will get fixed shortly.) I don't think it's unreasonable to remove that comment and just leave them as visable functions. Do you want another patch to remove the comment? Anyone have feelings that we shouldn't export such a general interface?

I'm not sure what we gain from such an interface for users of the library, as it requires the caller to know the full path of the proc file, so they have to already know about /proc/pid/attr and about the specific node they want. At which point they might as well just open and read it directly (no added abstraction). So I'd hide these functions within the library (i.e. hidden qualifier), not make them part of the public interface.

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 34 lines · Stephen Smalley
Quoted 22 lines · Eric Paris
Quoted 6 lines · Stephen Smalley
Quoted 6 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +0 -0
@@ -24,6 +24,14 @@ extern void freeconary(security_context_
 
 /* Wrappers for the /proc/pid/attr API. */
 
Quoted 14 lines

include/selinux/selinux.h extern void freeconary(security_context_ · line 37

Quoted 11 lines
include/selinux/selinux.h renamed from include/selinux/selinux.h.p1 +8 -0
+/* 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);

If they should only be called from inside libselinux, why are they defined in selinux/selinux.h, and exported to users of the library?

Quoted 10 lines

When I originally typed that comment I was still under the misguided impression they were being hidden (which should have stood out as strange for the same reason it did to james). I could see value if you had done something custom to your kernel or your libselinux didn't have interfaces for all of the /proc/pid/attr/* (as is now the case since there are kernels with keycreate and sockcreate but no libselinux interface, even if that will get fixed shortly.) I don't think it's unreasonable to remove that comment and just leave them as visable functions. Do you want another patch to remove the comment? Anyone have feelings that we shouldn't export such a general interface?

Quoted 7 lines

I'm not sure what we gain from such an interface for users of the library, as it requires the caller to know the full path of the proc file, so they have to already know about /proc/pid/attr and about the specific node they want. At which point they might as well just open and read it directly (no added abstraction). So I'd hide these functions within the library (i.e. hidden qualifier), not make them part of the public interface.

I merged the patches, with the above change ([gs]etprocattrcon are now truly hidden functions within the shared library).

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.