Labelset support to xattr API.
authorJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Wed, 24 Nov 2010 00:43:47 +0000 (16:43 -0800)
committerJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Wed, 24 Nov 2010 00:43:47 +0000 (16:43 -0800)
src/smack.h
src/smack_xattr.c
tests/check_xattr.c

index 3160cca..776ff36 100644 (file)
@@ -169,10 +169,12 @@ extern int smack_rule_set_have_access(SmackRuleSet handle, const char *subject,
  * @param path path to a file
  * @param attr attribute name
  * @param smack new value
+ * @param labels label set. Not used if set to NULL. Otherwise, converts
+ * to short name.
  * @return 0 on success
  */
 extern int smack_xattr_set_to_file(const char *path, const char *attr,
-                                  const char *smack);
+                                  const char *smack, SmackLabelSet labels);
 
 /*!
  * Get SMACK64 security attribute for a given path.
@@ -181,19 +183,24 @@ extern int smack_xattr_set_to_file(const char *path, const char *attr,
  * @param path path to a file
  * @param attr attribute name
  * @param smack current value
+ * @param labels label set. Not used if set to NULL. Otherwise, converts
+ * to long name.
  * @return 0 on success
  */
 extern int smack_xattr_get_from_file(const char *path, const char *attr,
-                                    char **smack);
+                                    char **smack, SmackLabelSet labels);
 
 /*!
  * Get SMACK64 security attribute for a given pid.
  *
  * @param pid pid of a process
  * @param smack current value
+ * @param labels label set. Not used if set to NULL. Otherwise, converts
+ * to long name.
  * @return 0 on success
  */
-extern int smack_xattr_get_from_proc(int pid, char **smack);
+extern int smack_xattr_get_from_proc(int pid, char **smack,
+                                    SmackLabelSet labels);
 
 /*!
  * Create a new label set. The returned rule set must be freed with
index 0fcf93d..9cf09d6 100644 (file)
 #define SMACK_PROC_PATH "/proc/%d/attr/current"
 #define LINE_BUFFER_SIZE 255
 
-
-int smack_xattr_set_to_file(const char *path, const char *attr, const char *smack)
+int smack_xattr_set_to_file(const char *path, const char *attr,
+                           const char *smack, SmackLabelSet labels)
 {
        size_t size;
        int ret;
 
+       if (labels != NULL)
+               smack = smack_label_set_to_short_name(labels, smack);
+
+       if (smack == NULL)
+               return -1;
+
        size = strlen(smack);
        if (size > SMACK64_LEN)
                return -1;
@@ -48,33 +54,35 @@ int smack_xattr_set_to_file(const char *path, const char *attr, const char *smac
        return ret;
 }
 
-int smack_xattr_get_from_file(const char *path, const char *attr, char **smack)
+int smack_xattr_get_from_file(const char *path, const char *attr,
+                             char **smack, SmackLabelSet labels)
 {
        ssize_t ret;
-       char *buf;
+       char short_name[SMACK64_LEN + 2];
+       const char *result;
 
-       ret = getxattr(path, attr, NULL, 0);
+       ret = getxattr(path, attr, short_name, SMACK64_LEN + 1);
        if (ret < 0)
                return -1;
 
-       buf = malloc(ret + 1);
+       short_name[ret] = '\0';
 
-       ret = getxattr(path, attr, buf, ret);
-       if (ret < 0) {
-               free(buf);
+       if (labels == NULL)
+               result = short_name;
+       else
+               result = smack_label_set_to_long_name(labels, short_name);
+
+       if (result == NULL)
                return -1;
-       }
 
-       buf[ret] = '\0';
-       *smack = buf;
-       return 0;
+       *smack = strdup(result);
+       if (*smack == NULL)
+               return -1;
 
+       return 0;
 }
 
-
-
-
-int smack_xattr_get_from_proc(int pid, char **smack)
+int smack_xattr_get_from_proc(int pid, char **smack, SmackLabelSet labels)
 {
        char buf[LINE_BUFFER_SIZE];
        FILE *file;
@@ -94,3 +102,4 @@ int smack_xattr_get_from_proc(int pid, char **smack)
        *smack = strdup(buf);
        return *smack != NULL ? 0 : - 1;
 }
+
index a7f83dd..7317656 100644 (file)
 #include <check.h>
 #include "../src/smack.h"
 
+#define LONG_LABEL_1 "FooFooFooFooFooFooFooFooFooFooFooFooFoo"
+
 static int files_equal(const char *filename1, const char *filename2);
 
-START_TEST(test_set_smack_to_file)
+START_TEST(test_xattr_set_to_file_smack)
 {
        FILE *file;
        int rc = 0;
@@ -37,10 +39,10 @@ START_TEST(test_set_smack_to_file)
        fprintf(file, "dummy\n");
        fclose(file);
 
-       rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64, "Apple");
+       rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64, "Apple", NULL);
        fail_unless(rc == 0, "Failed to set SMACK64");
 
-       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, &smack);
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, &smack, NULL);
        fail_unless(rc == 0, "Failed to get SMACK64");
 
        rc = strcmp(smack, "Apple");
@@ -50,7 +52,7 @@ START_TEST(test_set_smack_to_file)
 }
 END_TEST
 
-START_TEST(test_set_smackexec_to_file)
+START_TEST(test_xattr_set_to_file_smackexec)
 {
        FILE *file;
        int rc;
@@ -60,10 +62,10 @@ START_TEST(test_set_smackexec_to_file)
        fprintf(file, "dummy\n");
        fclose(file);
 
-       rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64EXEC, "Apple");
+       rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64EXEC, "Apple", NULL);
        fail_unless(rc == 0, "Failed to set SMACK64EXEC");
 
-       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64EXEC, &smack);
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64EXEC, &smack, NULL);
        fail_unless(rc == 0, "Failed to get SMACK64EXEC");
 
        rc = strcmp(smack, "Apple");
@@ -73,6 +75,38 @@ START_TEST(test_set_smackexec_to_file)
 }
 END_TEST
 
+START_TEST(test_xattr_set_to_file_smack_long_label)
+{
+       FILE *file;
+       int rc = 0;
+       SmackLabelSet labels;
+       char *smack = NULL;
+
+       file = fopen("set_smack-dummy.txt", "w");
+       fprintf(file, "dummy\n");
+       fclose(file);
+
+       labels = smack_label_set_new();
+       fail_unless(labels != NULL, "Creating label set failed");
+
+       smack_label_set_add(labels, LONG_LABEL_1);
+       fail_unless(rc == 0, "Adding label was not succesful");
+
+       rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64, LONG_LABEL_1, labels);
+       fail_unless(rc == 0, "Failed to set SMACK64");
+
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, &smack, labels);
+       fail_unless(rc == 0, "Failed to get SMACK64");
+
+       rc = strcmp(smack, LONG_LABEL_1);
+       fail_unless(rc == 0, "smack %s not equal to Apple", smack);
+
+       free(smack);
+
+       smack_label_set_delete(labels);
+}
+END_TEST
+
 Suite *ruleset_suite (void)
 {
        Suite *s;
@@ -81,8 +115,9 @@ Suite *ruleset_suite (void)
        s = suite_create("Xattr");
 
        tc_core = tcase_create("Xattr");
-       tcase_add_test(tc_core, test_set_smack_to_file);
-       tcase_add_test(tc_core, test_set_smackexec_to_file);
+       tcase_add_test(tc_core, test_xattr_set_to_file_smack);
+       tcase_add_test(tc_core, test_xattr_set_to_file_smackexec);
+       tcase_add_test(tc_core, test_xattr_set_to_file_smack_long_label);
        suite_add_tcase(s, tc_core);
 
        return s;