* @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.
* @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
#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;
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;
*smack = strdup(buf);
return *smack != NULL ? 0 : - 1;
}
+
#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;
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");
}
END_TEST
-START_TEST(test_set_smackexec_to_file)
+START_TEST(test_xattr_set_to_file_smackexec)
{
FILE *file;
int rc;
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");
}
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;
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;