Changed check_xattr API not to do memory allocation.
authorJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Fri, 14 Jan 2011 12:09:05 +0000 (04:09 -0800)
committerJarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Fri, 14 Jan 2011 12:09:05 +0000 (04:09 -0800)
Simpler to use now from libcreds3.

src/smack.h
src/smack_xattr.c
tests/check_xattr.c

index 2fbf1dc..f6e6413 100644 (file)
@@ -32,6 +32,8 @@
 #ifndef SMACK_H
 #define SMACK_H
 
+#include <sys/types.h>
+
 /*!
  * Smack config file default paths.
  */
@@ -310,25 +312,29 @@ 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 smack attribute value
+ * @param size size of the character array reserved for the 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, SmackLabelSet labels);
+extern ssize_t smack_xattr_get_from_file(const char *path, const char *attr,
+                                        char *smack, size_t size,
+                                        SmackLabelSet labels);
 
 /*!
  * Get SMACK64 security attribute for a given pid.
  *
  * @param pid pid of a process
- * @param smack current value
+ * @param smack attribute value
+ * @param size size of the character array reserved for the 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,
-                                    SmackLabelSet labels);
+extern ssize_t smack_xattr_get_from_proc(int pid, char *smack,
+                                        size_t size,
+                                        SmackLabelSet labels);
 
 #ifdef __cplusplus
 }
index 36e8f4f..e06b556 100644 (file)
@@ -51,52 +51,78 @@ int smack_xattr_set_to_file(const char *path, const char *attr,
        return ret;
 }
 
-int smack_xattr_get_from_file(const char *path, const char *attr,
-                             char **smack, SmackLabelSet labels)
+ssize_t smack_xattr_get_from_file(const char *path, const char *attr,
+                                 char *smack, size_t size, SmackLabelSet labels)
 {
        ssize_t ret;
-       char short_name[SMACK64_LEN + 2];
+       char buf[SMACK64_LEN + 2];
        const char *result;
+       size_t rsize;
 
-       ret = getxattr(path, attr, short_name, SMACK64_LEN + 1);
+       ret = getxattr(path, attr, buf, SMACK64_LEN + 1);
        if (ret < 0)
                return -1;
 
-       short_name[ret] = '\0';
+       buf[ret] = '\0';
 
        if (labels == NULL)
-               result = short_name;
+               result = buf;
        else
-               result = smack_label_set_to_long_name(labels, short_name);
+               result = smack_label_set_to_long_name(labels, buf);
 
        if (result == NULL)
                return -1;
 
-       *smack = strdup(result);
-       if (*smack == NULL)
+       rsize = strlen(result) + 1;
+
+       if (smack == NULL)
+               return rsize;
+       else if (size < rsize)
                return -1;
 
+       strcpy(smack, result);
+
        return 0;
 }
 
-int smack_xattr_get_from_proc(int pid, char **smack, SmackLabelSet labels)
+ssize_t smack_xattr_get_from_proc(int pid, char *smack,
+                                 size_t size,
+                                 SmackLabelSet labels)
 {
        char buf[512];
        FILE *file;
+       const char *result;
+       size_t rsize;
 
-       snprintf(buf, sizeof(buf), SMACK_PROC_PATH, pid);
+       snprintf(buf, sizeof(result), SMACK_PROC_PATH, pid);
 
        file = fopen(buf, "r");
        if (file == NULL)
                return -1;
 
-       if (fgets(buf, sizeof(buf), file) == NULL) {
+       if (fgets(buf, sizeof(result), file) == NULL) {
                fclose(file);
                return -1;
        }
 
        fclose(file);
-       *smack = strdup(buf);
-       return *smack != NULL ? 0 : - 1;
+
+       if (labels == NULL)
+               result = buf;
+       else
+               result = smack_label_set_to_long_name(labels, buf);
+
+       if (result == NULL)
+               return -1;
+
+       rsize = strlen(result) + 1;
+
+       if (smack == NULL)
+               return rsize;
+       else if (size < rsize)
+               return -1;
+
+       strcpy(smack, result);
+       return 0;
 }
 
index 7317656..400528d 100644 (file)
@@ -33,7 +33,7 @@ START_TEST(test_xattr_set_to_file_smack)
 {
        FILE *file;
        int rc = 0;
-       char *smack = NULL;
+       char smack[100];
 
        file = fopen("set_smack-dummy.txt", "w");
        fprintf(file, "dummy\n");
@@ -42,13 +42,11 @@ START_TEST(test_xattr_set_to_file_smack)
        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, NULL);
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, smack, 100, NULL);
        fail_unless(rc == 0, "Failed to get SMACK64");
 
        rc = strcmp(smack, "Apple");
        fail_unless(rc == 0, "smack %s not equal to Apple", smack);
-
-       free(smack);
 }
 END_TEST
 
@@ -56,7 +54,7 @@ START_TEST(test_xattr_set_to_file_smackexec)
 {
        FILE *file;
        int rc;
-       char *smack = NULL;
+       char smack[100];
 
        file = fopen("set_smack-dummy.txt", "w");
        fprintf(file, "dummy\n");
@@ -65,13 +63,11 @@ START_TEST(test_xattr_set_to_file_smackexec)
        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, NULL);
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64EXEC, smack, 100, NULL);
        fail_unless(rc == 0, "Failed to get SMACK64EXEC");
 
        rc = strcmp(smack, "Apple");
        fail_unless(rc == 0, "smack %s not equal to Apple", smack);
-
-       free(smack);
 }
 END_TEST
 
@@ -80,7 +76,7 @@ START_TEST(test_xattr_set_to_file_smack_long_label)
        FILE *file;
        int rc = 0;
        SmackLabelSet labels;
-       char *smack = NULL;
+       char smack[100];
 
        file = fopen("set_smack-dummy.txt", "w");
        fprintf(file, "dummy\n");
@@ -95,14 +91,12 @@ START_TEST(test_xattr_set_to_file_smack_long_label)
        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);
+       rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, smack, 100, 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