|
| 1 | +/* |
| 2 | +* Copyright 2020 The SELint Contributors |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "naming.h" |
| 18 | + |
| 19 | +#include <ctype.h> |
| 20 | +#include <stddef.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +static const char *const reason_invalid_char = "contains invalid character"; |
| 24 | +static const char *const reason_start_underscore = "starts with an underscore"; |
| 25 | +static const char *const reason_consecutive_underscores = "contains consecutive underscores"; |
| 26 | +static const char *const reason_end_underscore = "ends with an underscore"; |
| 27 | +static const char *const reason_type_postfix = "type does not end with type-specific postfix"; |
| 28 | +static const char *const reason_role_postfix = "role does not end with role-specific postfix"; |
| 29 | +static const char *const reason_user_postfix = "user does not end with user-specific postfix"; |
| 30 | +static const char *const reason_if_no_mod_prefix = "interface has no module name prefix"; |
| 31 | +static const char *const reason_if_no_postfix = "interface has no postfix"; |
| 32 | +static const char *const reason_if_invalid_postfix = "interface has an invalid postfix"; |
| 33 | +static const char *const reason_temp_no_mod_prefix = "template has no module name prefix"; |
| 34 | +static const char *const reason_temp_no_postfix = "template has no postfix"; |
| 35 | +static const char *const reason_temp_invalid_postfix = "template has an invalid postfix"; |
| 36 | + |
| 37 | + |
| 38 | +static const char *generic_check(const char *name) |
| 39 | +{ |
| 40 | + const unsigned char *c = (const unsigned char *)name; |
| 41 | + |
| 42 | + if (*c == '_') { |
| 43 | + return reason_start_underscore; |
| 44 | + } |
| 45 | + |
| 46 | + unsigned short prev_is_underscore = 0; |
| 47 | + while (*c != '\0') { |
| 48 | + if (!islower(*c) && !isdigit(*c) && *c != '_') { |
| 49 | + return reason_invalid_char; |
| 50 | + } |
| 51 | + |
| 52 | + if (*c == '_') { |
| 53 | + if (prev_is_underscore) { |
| 54 | + return reason_consecutive_underscores; |
| 55 | + } |
| 56 | + |
| 57 | + prev_is_underscore = 1; |
| 58 | + } else { |
| 59 | + prev_is_underscore = 0; |
| 60 | + } |
| 61 | + |
| 62 | + c++; |
| 63 | + } |
| 64 | + |
| 65 | + if (*(c-1) == '_') { |
| 66 | + return reason_end_underscore; |
| 67 | + } |
| 68 | + |
| 69 | + return NULL; |
| 70 | +} |
| 71 | + |
| 72 | +static const char *type_check(const char *name) |
| 73 | +{ |
| 74 | + const size_t name_len = strlen(name); |
| 75 | + if (name_len < 2 || name[name_len-2] != '_' || name[name_len-1] != 't') { |
| 76 | + return reason_type_postfix; |
| 77 | + } |
| 78 | + |
| 79 | + return NULL; |
| 80 | +} |
| 81 | + |
| 82 | +static const char *role_check(const char *name) |
| 83 | +{ |
| 84 | + const size_t name_len = strlen(name); |
| 85 | + if (name_len < 2 || name[name_len-2] != '_' || name[name_len-1] != 'r') { |
| 86 | + return reason_role_postfix; |
| 87 | + } |
| 88 | + |
| 89 | + return NULL; |
| 90 | +} |
| 91 | + |
| 92 | +static const char *user_check(const char *name) |
| 93 | +{ |
| 94 | + const size_t name_len = strlen(name); |
| 95 | + if (name_len < 2 || name[name_len-2] != '_' || name[name_len-1] != 'u') { |
| 96 | + return reason_user_postfix; |
| 97 | + } |
| 98 | + |
| 99 | + return NULL; |
| 100 | +} |
| 101 | + |
| 102 | +const char *naming_decl_check(const char *name, enum decl_flavor flavor) |
| 103 | +{ |
| 104 | + // class and permission names are not in the scope of a policy writer |
| 105 | + if (flavor == DECL_CLASS || flavor == DECL_PERM) { |
| 106 | + return NULL; |
| 107 | + } |
| 108 | + |
| 109 | + const char *res = generic_check(name); |
| 110 | + if (res) { |
| 111 | + return res; |
| 112 | + } |
| 113 | + |
| 114 | + switch (flavor) { |
| 115 | + case DECL_TYPE: |
| 116 | + res = type_check(name); |
| 117 | + break; |
| 118 | + case DECL_ATTRIBUTE: |
| 119 | + //TODO |
| 120 | + break; |
| 121 | + case DECL_ROLE: |
| 122 | + res = role_check(name); |
| 123 | + break; |
| 124 | + case DECL_ATTRIBUTE_ROLE: |
| 125 | + //TODO |
| 126 | + break; |
| 127 | + case DECL_USER: |
| 128 | + res = user_check(name); |
| 129 | + break; |
| 130 | + case DECL_BOOL: |
| 131 | + //TODO |
| 132 | + break; |
| 133 | + case DECL_CLASS: |
| 134 | + case DECL_PERM: |
| 135 | + break; |
| 136 | + } |
| 137 | + |
| 138 | + if (res) { |
| 139 | + return res; |
| 140 | + } |
| 141 | + |
| 142 | + return NULL; |
| 143 | +} |
| 144 | + |
| 145 | +static int mod_prefix_check(const char *name, const char *mod_name) |
| 146 | +{ |
| 147 | + const size_t mod_name_len = strlen(mod_name); |
| 148 | + |
| 149 | + if (0 == strncmp(name, mod_name, mod_name_len) && name[mod_name_len] == '_') { |
| 150 | + return 0; |
| 151 | + } |
| 152 | + |
| 153 | + static const char *const exceptions[][2] = { |
| 154 | + { "fs" , "filesystem" }, |
| 155 | + { "corecmd" , "corecommands" }, |
| 156 | + { "seutil" , "selinuxutil" }, |
| 157 | + { "libs" , "libraries" }, |
| 158 | + { "dev" , "devices" }, |
| 159 | + { "term" , "terminal" }, |
| 160 | + { "corenet" , "corenetwork" }, |
| 161 | + { "auth" , "authlogin" }, |
| 162 | + { "userdom" , "userdomain" }, |
| 163 | + { "sysnet" , "sysnetwork" }, |
| 164 | + }; |
| 165 | + |
| 166 | + const char *prefix = strchr(name, '_'); |
| 167 | + if (!prefix) { |
| 168 | + return 1; |
| 169 | + } |
| 170 | + |
| 171 | + const size_t prefix_len = prefix - name; |
| 172 | + |
| 173 | + size_t i; |
| 174 | + for (i = 0; i < (sizeof exceptions / sizeof *exceptions); ++i) { |
| 175 | + if (0 == strncmp(name, exceptions[i][0], prefix_len) && |
| 176 | + 0 == strcmp(mod_name, exceptions[i][1])) { |
| 177 | + return 0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return 1; |
| 182 | +} |
| 183 | + |
| 184 | +const char *naming_if_check(const char *name, const char *module_name) |
| 185 | +{ |
| 186 | + const char *res = generic_check(name); |
| 187 | + if (res) { |
| 188 | + return res; |
| 189 | + } |
| 190 | + |
| 191 | + if (mod_prefix_check(name, module_name)) { |
| 192 | + return reason_if_no_mod_prefix; |
| 193 | + } |
| 194 | + |
| 195 | + const char *postfix = strrchr(name, '_'); |
| 196 | + if (!postfix) { |
| 197 | + return reason_if_no_postfix; |
| 198 | + } |
| 199 | + |
| 200 | + static const char *const invalid_postfixes[] = { |
| 201 | + "_pattern", |
| 202 | + }; |
| 203 | + size_t i; |
| 204 | + for (i = 0; i < (sizeof invalid_postfixes / sizeof *invalid_postfixes); ++i) { |
| 205 | + if (0 == strcmp(postfix, invalid_postfixes[i])) { |
| 206 | + return reason_if_invalid_postfix; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return NULL; |
| 211 | +} |
| 212 | + |
| 213 | +const char *naming_temp_check(const char *name, const char *module_name) |
| 214 | +{ |
| 215 | + const char *res = generic_check(name); |
| 216 | + if (res) { |
| 217 | + return res; |
| 218 | + } |
| 219 | + |
| 220 | + if (mod_prefix_check(name, module_name)) { |
| 221 | + return reason_temp_no_mod_prefix; |
| 222 | + } |
| 223 | + |
| 224 | + const char *postfix = strrchr(name, '_'); |
| 225 | + if (!postfix) { |
| 226 | + return reason_temp_no_postfix; |
| 227 | + } |
| 228 | + |
| 229 | + if (0 != strcmp(postfix, "_template") && |
| 230 | + 0 != strcmp(postfix, "_admin")) { |
| 231 | + return reason_temp_invalid_postfix; |
| 232 | + } |
| 233 | + |
| 234 | + return NULL; |
| 235 | +} |
0 commit comments