Copy labels using get_label()
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tue, 19 Nov 2013 13:07:17 +0000 (15:07 +0200)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Wed, 20 Nov 2013 07:49:44 +0000 (09:49 +0200)
Copy and validate labels in a single transaction:

- No trust for having '\0' in the src buffer when copying
  labels.
- Improves performance by combining length calculation, validation
  and copying.
(cherry picked from commit 16f84d57e2766f1ccfd59ae77fad407b6cc5ff81)

libsmack/libsmack.c

index 205d622..192a2a0 100644 (file)
@@ -172,16 +172,16 @@ int smack_accesses_add(struct smack_accesses *handle, const char *subject,
 {
        struct smack_rule *rule = NULL;
 
-       if (smack_label_length(subject) < 0 ||
-           smack_label_length(object) < 0)
-               return -1;
-
        rule = calloc(sizeof(struct smack_rule), 1);
        if (rule == NULL)
                return -1;
 
-       strncpy(rule->subject, subject, SMACK_LABEL_LEN);
-       strncpy(rule->object, object, SMACK_LABEL_LEN);
+       if (get_label(rule->subject, subject) < 0 ||
+           get_label(rule->object, object) < 0) {
+               free(rule);
+               return -1;
+       }
+
        parse_access_type(access_type, rule->access_type);
 
        if (handle->first == NULL) {
@@ -202,16 +202,16 @@ int smack_accesses_add_modify(struct smack_accesses *handle,
 {
        struct smack_rule *rule = NULL;
 
-       if (smack_label_length(subject) < 0 ||
-           smack_label_length(object) < 0)
-               return -1;
-
        rule = calloc(sizeof(struct smack_rule), 1);
        if (rule == NULL)
                return -1;
 
-       strncpy(rule->subject, subject, SMACK_LABEL_LEN);
-       strncpy(rule->object, object, SMACK_LABEL_LEN);
+       if (get_label(rule->subject, subject) < 0 ||
+           get_label(rule->object, object) < 0) {
+               free(rule);
+               return -1;
+       }
+
        parse_access_type(allow_access_type, rule->allow_access_type);
        parse_access_type(deny_access_type, rule->deny_access_type);
        rule->is_modify = 1;
@@ -431,10 +431,9 @@ int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
                label = strtok_r(buf, " \t\n", &ptr);
                level = strtok_r(NULL, " \t\n", &ptr);
                cat = strtok_r(NULL, " \t\n", &ptr);
-               if (smack_label_length(label) < 0 || level == NULL)
-                       goto err_out;
 
-               strncpy(mapping->label, label, SMACK_LABEL_LEN);
+               if (level == NULL || get_label(mapping->label, label) < 0)
+                       goto err_out;
 
                errno = 0;
                val = strtol(level, NULL, 10);
@@ -572,7 +571,7 @@ int smack_set_label_for_self(const char *label)
        int fd;
        int ret;
 
-       len = smack_label_length(label);
+       len = get_label(NULL, label);
        if (len < 0)
                return -1;
 
@@ -593,7 +592,7 @@ int smack_revoke_subject(const char *subject)
        int len;
        char path[PATH_MAX];
 
-       len = smack_label_length(subject);
+       len = get_label(NULL, subject);
        if (len < 0)
                return -1;