Helper function get_label()
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tue, 19 Nov 2013 11:49:37 +0000 (13:49 +0200)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Wed, 20 Nov 2013 07:49:06 +0000 (09:49 +0200)
The helper function ssize_t get_label(char *dest, const char *src)
validates the given label and copies it to the dest buffer if
available.
(cherry picked from commit 09fdff9a456dec54a4c8548c9b9acbcbce48d59a)

libsmack/libsmack.c

index 83988f1..205d622 100644 (file)
@@ -84,7 +84,8 @@ struct smack_cipso {
 
 static int accesses_apply(struct smack_accesses *handle, int clear);
 static inline void parse_access_type(const char *in, char out[ACC_LEN + 1]);
-static int smack_label_length(const char *label) __attribute__((unused));
+static ssize_t smack_label_length(const char *label) __attribute__((unused));
+static inline ssize_t get_label(char *dest, const char *src);
 
 int smack_accesses_new(struct smack_accesses **accesses)
 {
@@ -607,6 +608,11 @@ int smack_revoke_subject(const char *subject)
        return (ret < 0) ? -1 : 0;
 }
 
+ssize_t smack_label_length(const char *label)
+{
+       return get_label(NULL, label);
+}
+
 static int accesses_apply(struct smack_accesses *handle, int clear)
 {
        char buf[LOAD_LEN + 1];
@@ -718,15 +724,15 @@ static inline void parse_access_type(const char *in, char out[ACC_LEN + 1])
                }
 }
 
-static int smack_label_length(const char *label)
+static inline ssize_t get_label(char *dest, const char *src)
 {
        int i;
 
-       if (!label || label[0] == '\0' || label[0] == '-')
+       if (!src || src[0] == '\0' || src[0] == '-')
                return -1;
 
-       for (i = 0; i < (SMACK_LABEL_LEN + 1) && label[i]; i++) {
-               switch (label[i]) {
+       for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
+               switch (src[i]) {
                case ' ':
                case '/':
                case '"':
@@ -736,7 +742,13 @@ static int smack_label_length(const char *label)
                default:
                        break;
                }
+
+               if (dest)
+                       dest[i] = src[i];
        }
 
+       if (i < (SMACK_LABEL_LEN + 1))
+               dest[i] = '\0';
+
        return i < (SMACK_LABEL_LEN + 1) ? i : -1;
 }