Add get_self_label functionality
authorBrian McGillion <brian.mcgillion@intel.com>
Wed, 2 Nov 2011 14:52:08 +0000 (16:52 +0200)
committerBrian McGillion <brian.mcgillion@intel.com>
Wed, 2 Nov 2011 14:52:08 +0000 (16:52 +0200)
src/smack.c
src/smack.h
tests/Makefile.am
tests/smackselflabel.c [new file with mode: 0644]

index a90b2bc..e577a0a 100644 (file)
@@ -46,6 +46,7 @@
 #define KERNEL_FORMAT "%-23s %-23s %5s"
 #define READ_BUF_SIZE 512
 #define SMACKFS_MNT "/smack"
+#define SELF_LABEL_FILE "/proc/self/attr/current"
 
 struct smack_rule {
        char subject[LABEL_LEN + 1];
@@ -287,6 +288,32 @@ char *smack_get_peer_label(int fd)
        return label;
 }
 
+char *smack_get_self_label()
+{
+       char *label;
+       int fd;
+       int ret;
+
+       label = calloc(LABEL_LEN + 1, 1);
+       if (label == NULL)
+               return NULL;
+
+       fd = open(SELF_LABEL_FILE, O_RDONLY);
+       if (fd < 0) {
+               free(label);
+               return NULL;
+       }
+
+       ret = read(fd, label, LABEL_LEN);
+       close(fd);
+       if (ret < 0) {
+               free(label);
+               return NULL;
+       }
+
+       return label;
+}
+
 inline int access_type_to_int(const char *access_type)
 {
        int i, count;
index 8271e3e..5e097ad 100644 (file)
@@ -114,6 +114,13 @@ extern int smack_have_access(const char *subject, const char *object,
   */
 extern char *smack_get_peer_label(int fd);
 
+/*!
+  * Allow a process to determine its own smack label
+  *
+  * @return The label of the process, NULL on error.
+  */
+extern char *smack_get_self_label();
+
 #ifdef __cplusplus
 }
 #endif
index bd9c0d6..ac4cd24 100644 (file)
@@ -1,9 +1,12 @@
 AM_CPPFLAGS = -I../src
 
-noinst_PROGRAMS = smackctl smackaccess
+noinst_PROGRAMS = smackctl smackaccess smackselflabel
 
 smackctl_SOURCES = smackctl.c
 smackctl_LDADD = -L$(top_builddir)/src/.libs -lsmack
 
 smackaccess_SOURCES = smackaccess.c
 smackaccess_LDADD = -L$(top_builddir)/src/.libs -lsmack
+
+smackselflabel_SOURCES = smackselflabel.c
+smackselflabel_LDADD = -L$(top_builddir)/src/.libs -lsmack
diff --git a/tests/smackselflabel.c b/tests/smackselflabel.c
new file mode 100644 (file)
index 0000000..ad0eb2e
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file is part of libsmack
+ *
+ * Copyright (C) 2011 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * Authors:
+ * Brian McGillion <brian.mcgillion@intel.com>
+ */
+
+#include <smack.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+       char *own_label;
+
+       own_label = smack_get_self_label();
+       if (own_label)
+               printf("Own_label: '%s'\n", own_label);
+       else
+               printf("Error reading label\n");
+
+       free(own_label);
+
+       return EXIT_SUCCESS;
+}