From 0f6cb7df9a2c9c5dc8c0ebd40b42e5a4a04ce1c2 Mon Sep 17 00:00:00 2001 From: Brian McGillion Date: Wed, 2 Nov 2011 16:52:08 +0200 Subject: [PATCH] Add get_self_label functionality --- src/smack.c | 27 +++++++++++++++++++++++++++ src/smack.h | 7 +++++++ tests/Makefile.am | 5 ++++- tests/smackselflabel.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/smackselflabel.c diff --git a/src/smack.c b/src/smack.c index a90b2bc..e577a0a 100644 --- a/src/smack.c +++ b/src/smack.c @@ -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; diff --git a/src/smack.h b/src/smack.h index 8271e3e..5e097ad 100644 --- a/src/smack.h +++ b/src/smack.h @@ -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 diff --git a/tests/Makefile.am b/tests/Makefile.am index bd9c0d6..ac4cd24 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000..ad0eb2e --- /dev/null +++ b/tests/smackselflabel.c @@ -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 + */ + +#include +#include +#include + +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; +} -- 2.7.4