From c86ec5e55b0734b7b8c2381a3bbe4a9d52b54b03 Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Tue, 19 Nov 2013 13:49:37 +0200 Subject: [PATCH] Helper function get_label() 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 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/libsmack/libsmack.c b/libsmack/libsmack.c index 83988f1..205d622 100644 --- a/libsmack/libsmack.c +++ b/libsmack/libsmack.c @@ -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; } -- 2.7.4