| .. | .. |
|---|
| 10 | 10 | #include <linux/kernel.h> |
|---|
| 11 | 11 | #include <linux/errno.h> |
|---|
| 12 | 12 | #include <linux/jiffies.h> |
|---|
| 13 | +#include <linux/nospec.h> |
|---|
| 13 | 14 | #include <linux/skbuff.h> |
|---|
| 14 | 15 | #include <linux/string.h> |
|---|
| 15 | 16 | #include <linux/types.h> |
|---|
| .. | .. |
|---|
| 44 | 45 | [NLA_S64] = sizeof(s64), |
|---|
| 45 | 46 | }; |
|---|
| 46 | 47 | |
|---|
| 48 | +/* |
|---|
| 49 | + * Nested policies might refer back to the original |
|---|
| 50 | + * policy in some cases, and userspace could try to |
|---|
| 51 | + * abuse that and recurse by nesting in the right |
|---|
| 52 | + * ways. Limit recursion to avoid this problem. |
|---|
| 53 | + */ |
|---|
| 54 | +#define MAX_POLICY_RECURSION_DEPTH 10 |
|---|
| 55 | + |
|---|
| 56 | +static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
|---|
| 57 | + const struct nla_policy *policy, |
|---|
| 58 | + unsigned int validate, |
|---|
| 59 | + struct netlink_ext_ack *extack, |
|---|
| 60 | + struct nlattr **tb, unsigned int depth); |
|---|
| 61 | + |
|---|
| 47 | 62 | static int validate_nla_bitfield32(const struct nlattr *nla, |
|---|
| 48 | | - u32 *valid_flags_allowed) |
|---|
| 63 | + const u32 valid_flags_mask) |
|---|
| 49 | 64 | { |
|---|
| 50 | 65 | const struct nla_bitfield32 *bf = nla_data(nla); |
|---|
| 51 | | - u32 *valid_flags_mask = valid_flags_allowed; |
|---|
| 52 | 66 | |
|---|
| 53 | | - if (!valid_flags_allowed) |
|---|
| 67 | + if (!valid_flags_mask) |
|---|
| 54 | 68 | return -EINVAL; |
|---|
| 55 | 69 | |
|---|
| 56 | 70 | /*disallow invalid bit selector */ |
|---|
| 57 | | - if (bf->selector & ~*valid_flags_mask) |
|---|
| 71 | + if (bf->selector & ~valid_flags_mask) |
|---|
| 58 | 72 | return -EINVAL; |
|---|
| 59 | 73 | |
|---|
| 60 | 74 | /*disallow invalid bit values */ |
|---|
| 61 | | - if (bf->value & ~*valid_flags_mask) |
|---|
| 75 | + if (bf->value & ~valid_flags_mask) |
|---|
| 62 | 76 | return -EINVAL; |
|---|
| 63 | 77 | |
|---|
| 64 | 78 | /*disallow valid bit values that are not selected*/ |
|---|
| .. | .. |
|---|
| 68 | 82 | return 0; |
|---|
| 69 | 83 | } |
|---|
| 70 | 84 | |
|---|
| 71 | | -static int validate_nla(const struct nlattr *nla, int maxtype, |
|---|
| 72 | | - const struct nla_policy *policy) |
|---|
| 85 | +static int nla_validate_array(const struct nlattr *head, int len, int maxtype, |
|---|
| 86 | + const struct nla_policy *policy, |
|---|
| 87 | + struct netlink_ext_ack *extack, |
|---|
| 88 | + unsigned int validate, unsigned int depth) |
|---|
| 73 | 89 | { |
|---|
| 90 | + const struct nlattr *entry; |
|---|
| 91 | + int rem; |
|---|
| 92 | + |
|---|
| 93 | + nla_for_each_attr(entry, head, len, rem) { |
|---|
| 94 | + int ret; |
|---|
| 95 | + |
|---|
| 96 | + if (nla_len(entry) == 0) |
|---|
| 97 | + continue; |
|---|
| 98 | + |
|---|
| 99 | + if (nla_len(entry) < NLA_HDRLEN) { |
|---|
| 100 | + NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy, |
|---|
| 101 | + "Array element too short"); |
|---|
| 102 | + return -ERANGE; |
|---|
| 103 | + } |
|---|
| 104 | + |
|---|
| 105 | + ret = __nla_validate_parse(nla_data(entry), nla_len(entry), |
|---|
| 106 | + maxtype, policy, validate, extack, |
|---|
| 107 | + NULL, depth + 1); |
|---|
| 108 | + if (ret < 0) |
|---|
| 109 | + return ret; |
|---|
| 110 | + } |
|---|
| 111 | + |
|---|
| 112 | + return 0; |
|---|
| 113 | +} |
|---|
| 114 | + |
|---|
| 115 | +void nla_get_range_unsigned(const struct nla_policy *pt, |
|---|
| 116 | + struct netlink_range_validation *range) |
|---|
| 117 | +{ |
|---|
| 118 | + WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR && |
|---|
| 119 | + (pt->min < 0 || pt->max < 0)); |
|---|
| 120 | + |
|---|
| 121 | + range->min = 0; |
|---|
| 122 | + |
|---|
| 123 | + switch (pt->type) { |
|---|
| 124 | + case NLA_U8: |
|---|
| 125 | + range->max = U8_MAX; |
|---|
| 126 | + break; |
|---|
| 127 | + case NLA_U16: |
|---|
| 128 | + case NLA_BINARY: |
|---|
| 129 | + range->max = U16_MAX; |
|---|
| 130 | + break; |
|---|
| 131 | + case NLA_U32: |
|---|
| 132 | + range->max = U32_MAX; |
|---|
| 133 | + break; |
|---|
| 134 | + case NLA_U64: |
|---|
| 135 | + case NLA_MSECS: |
|---|
| 136 | + range->max = U64_MAX; |
|---|
| 137 | + break; |
|---|
| 138 | + default: |
|---|
| 139 | + WARN_ON_ONCE(1); |
|---|
| 140 | + return; |
|---|
| 141 | + } |
|---|
| 142 | + |
|---|
| 143 | + switch (pt->validation_type) { |
|---|
| 144 | + case NLA_VALIDATE_RANGE: |
|---|
| 145 | + case NLA_VALIDATE_RANGE_WARN_TOO_LONG: |
|---|
| 146 | + range->min = pt->min; |
|---|
| 147 | + range->max = pt->max; |
|---|
| 148 | + break; |
|---|
| 149 | + case NLA_VALIDATE_RANGE_PTR: |
|---|
| 150 | + *range = *pt->range; |
|---|
| 151 | + break; |
|---|
| 152 | + case NLA_VALIDATE_MIN: |
|---|
| 153 | + range->min = pt->min; |
|---|
| 154 | + break; |
|---|
| 155 | + case NLA_VALIDATE_MAX: |
|---|
| 156 | + range->max = pt->max; |
|---|
| 157 | + break; |
|---|
| 158 | + default: |
|---|
| 159 | + break; |
|---|
| 160 | + } |
|---|
| 161 | +} |
|---|
| 162 | + |
|---|
| 163 | +static int nla_validate_range_unsigned(const struct nla_policy *pt, |
|---|
| 164 | + const struct nlattr *nla, |
|---|
| 165 | + struct netlink_ext_ack *extack, |
|---|
| 166 | + unsigned int validate) |
|---|
| 167 | +{ |
|---|
| 168 | + struct netlink_range_validation range; |
|---|
| 169 | + u64 value; |
|---|
| 170 | + |
|---|
| 171 | + switch (pt->type) { |
|---|
| 172 | + case NLA_U8: |
|---|
| 173 | + value = nla_get_u8(nla); |
|---|
| 174 | + break; |
|---|
| 175 | + case NLA_U16: |
|---|
| 176 | + value = nla_get_u16(nla); |
|---|
| 177 | + break; |
|---|
| 178 | + case NLA_U32: |
|---|
| 179 | + value = nla_get_u32(nla); |
|---|
| 180 | + break; |
|---|
| 181 | + case NLA_U64: |
|---|
| 182 | + case NLA_MSECS: |
|---|
| 183 | + value = nla_get_u64(nla); |
|---|
| 184 | + break; |
|---|
| 185 | + case NLA_BINARY: |
|---|
| 186 | + value = nla_len(nla); |
|---|
| 187 | + break; |
|---|
| 188 | + default: |
|---|
| 189 | + return -EINVAL; |
|---|
| 190 | + } |
|---|
| 191 | + |
|---|
| 192 | + nla_get_range_unsigned(pt, &range); |
|---|
| 193 | + |
|---|
| 194 | + if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG && |
|---|
| 195 | + pt->type == NLA_BINARY && value > range.max) { |
|---|
| 196 | + pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
|---|
| 197 | + current->comm, pt->type); |
|---|
| 198 | + if (validate & NL_VALIDATE_STRICT_ATTRS) { |
|---|
| 199 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 200 | + "invalid attribute length"); |
|---|
| 201 | + return -EINVAL; |
|---|
| 202 | + } |
|---|
| 203 | + |
|---|
| 204 | + /* this assumes min <= max (don't validate against min) */ |
|---|
| 205 | + return 0; |
|---|
| 206 | + } |
|---|
| 207 | + |
|---|
| 208 | + if (value < range.min || value > range.max) { |
|---|
| 209 | + bool binary = pt->type == NLA_BINARY; |
|---|
| 210 | + |
|---|
| 211 | + if (binary) |
|---|
| 212 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 213 | + "binary attribute size out of range"); |
|---|
| 214 | + else |
|---|
| 215 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 216 | + "integer out of range"); |
|---|
| 217 | + |
|---|
| 218 | + return -ERANGE; |
|---|
| 219 | + } |
|---|
| 220 | + |
|---|
| 221 | + return 0; |
|---|
| 222 | +} |
|---|
| 223 | + |
|---|
| 224 | +void nla_get_range_signed(const struct nla_policy *pt, |
|---|
| 225 | + struct netlink_range_validation_signed *range) |
|---|
| 226 | +{ |
|---|
| 227 | + switch (pt->type) { |
|---|
| 228 | + case NLA_S8: |
|---|
| 229 | + range->min = S8_MIN; |
|---|
| 230 | + range->max = S8_MAX; |
|---|
| 231 | + break; |
|---|
| 232 | + case NLA_S16: |
|---|
| 233 | + range->min = S16_MIN; |
|---|
| 234 | + range->max = S16_MAX; |
|---|
| 235 | + break; |
|---|
| 236 | + case NLA_S32: |
|---|
| 237 | + range->min = S32_MIN; |
|---|
| 238 | + range->max = S32_MAX; |
|---|
| 239 | + break; |
|---|
| 240 | + case NLA_S64: |
|---|
| 241 | + range->min = S64_MIN; |
|---|
| 242 | + range->max = S64_MAX; |
|---|
| 243 | + break; |
|---|
| 244 | + default: |
|---|
| 245 | + WARN_ON_ONCE(1); |
|---|
| 246 | + return; |
|---|
| 247 | + } |
|---|
| 248 | + |
|---|
| 249 | + switch (pt->validation_type) { |
|---|
| 250 | + case NLA_VALIDATE_RANGE: |
|---|
| 251 | + range->min = pt->min; |
|---|
| 252 | + range->max = pt->max; |
|---|
| 253 | + break; |
|---|
| 254 | + case NLA_VALIDATE_RANGE_PTR: |
|---|
| 255 | + *range = *pt->range_signed; |
|---|
| 256 | + break; |
|---|
| 257 | + case NLA_VALIDATE_MIN: |
|---|
| 258 | + range->min = pt->min; |
|---|
| 259 | + break; |
|---|
| 260 | + case NLA_VALIDATE_MAX: |
|---|
| 261 | + range->max = pt->max; |
|---|
| 262 | + break; |
|---|
| 263 | + default: |
|---|
| 264 | + break; |
|---|
| 265 | + } |
|---|
| 266 | +} |
|---|
| 267 | + |
|---|
| 268 | +static int nla_validate_int_range_signed(const struct nla_policy *pt, |
|---|
| 269 | + const struct nlattr *nla, |
|---|
| 270 | + struct netlink_ext_ack *extack) |
|---|
| 271 | +{ |
|---|
| 272 | + struct netlink_range_validation_signed range; |
|---|
| 273 | + s64 value; |
|---|
| 274 | + |
|---|
| 275 | + switch (pt->type) { |
|---|
| 276 | + case NLA_S8: |
|---|
| 277 | + value = nla_get_s8(nla); |
|---|
| 278 | + break; |
|---|
| 279 | + case NLA_S16: |
|---|
| 280 | + value = nla_get_s16(nla); |
|---|
| 281 | + break; |
|---|
| 282 | + case NLA_S32: |
|---|
| 283 | + value = nla_get_s32(nla); |
|---|
| 284 | + break; |
|---|
| 285 | + case NLA_S64: |
|---|
| 286 | + value = nla_get_s64(nla); |
|---|
| 287 | + break; |
|---|
| 288 | + default: |
|---|
| 289 | + return -EINVAL; |
|---|
| 290 | + } |
|---|
| 291 | + |
|---|
| 292 | + nla_get_range_signed(pt, &range); |
|---|
| 293 | + |
|---|
| 294 | + if (value < range.min || value > range.max) { |
|---|
| 295 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 296 | + "integer out of range"); |
|---|
| 297 | + return -ERANGE; |
|---|
| 298 | + } |
|---|
| 299 | + |
|---|
| 300 | + return 0; |
|---|
| 301 | +} |
|---|
| 302 | + |
|---|
| 303 | +static int nla_validate_int_range(const struct nla_policy *pt, |
|---|
| 304 | + const struct nlattr *nla, |
|---|
| 305 | + struct netlink_ext_ack *extack, |
|---|
| 306 | + unsigned int validate) |
|---|
| 307 | +{ |
|---|
| 308 | + switch (pt->type) { |
|---|
| 309 | + case NLA_U8: |
|---|
| 310 | + case NLA_U16: |
|---|
| 311 | + case NLA_U32: |
|---|
| 312 | + case NLA_U64: |
|---|
| 313 | + case NLA_MSECS: |
|---|
| 314 | + case NLA_BINARY: |
|---|
| 315 | + return nla_validate_range_unsigned(pt, nla, extack, validate); |
|---|
| 316 | + case NLA_S8: |
|---|
| 317 | + case NLA_S16: |
|---|
| 318 | + case NLA_S32: |
|---|
| 319 | + case NLA_S64: |
|---|
| 320 | + return nla_validate_int_range_signed(pt, nla, extack); |
|---|
| 321 | + default: |
|---|
| 322 | + WARN_ON(1); |
|---|
| 323 | + return -EINVAL; |
|---|
| 324 | + } |
|---|
| 325 | +} |
|---|
| 326 | + |
|---|
| 327 | +static int nla_validate_mask(const struct nla_policy *pt, |
|---|
| 328 | + const struct nlattr *nla, |
|---|
| 329 | + struct netlink_ext_ack *extack) |
|---|
| 330 | +{ |
|---|
| 331 | + u64 value; |
|---|
| 332 | + |
|---|
| 333 | + switch (pt->type) { |
|---|
| 334 | + case NLA_U8: |
|---|
| 335 | + value = nla_get_u8(nla); |
|---|
| 336 | + break; |
|---|
| 337 | + case NLA_U16: |
|---|
| 338 | + value = nla_get_u16(nla); |
|---|
| 339 | + break; |
|---|
| 340 | + case NLA_U32: |
|---|
| 341 | + value = nla_get_u32(nla); |
|---|
| 342 | + break; |
|---|
| 343 | + case NLA_U64: |
|---|
| 344 | + value = nla_get_u64(nla); |
|---|
| 345 | + break; |
|---|
| 346 | + default: |
|---|
| 347 | + return -EINVAL; |
|---|
| 348 | + } |
|---|
| 349 | + |
|---|
| 350 | + if (value & ~(u64)pt->mask) { |
|---|
| 351 | + NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set"); |
|---|
| 352 | + return -EINVAL; |
|---|
| 353 | + } |
|---|
| 354 | + |
|---|
| 355 | + return 0; |
|---|
| 356 | +} |
|---|
| 357 | + |
|---|
| 358 | +static int validate_nla(const struct nlattr *nla, int maxtype, |
|---|
| 359 | + const struct nla_policy *policy, unsigned int validate, |
|---|
| 360 | + struct netlink_ext_ack *extack, unsigned int depth) |
|---|
| 361 | +{ |
|---|
| 362 | + u16 strict_start_type = policy[0].strict_start_type; |
|---|
| 74 | 363 | const struct nla_policy *pt; |
|---|
| 75 | 364 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
|---|
| 365 | + int err = -ERANGE; |
|---|
| 366 | + |
|---|
| 367 | + if (strict_start_type && type >= strict_start_type) |
|---|
| 368 | + validate |= NL_VALIDATE_STRICT; |
|---|
| 76 | 369 | |
|---|
| 77 | 370 | if (type <= 0 || type > maxtype) |
|---|
| 78 | 371 | return 0; |
|---|
| 79 | 372 | |
|---|
| 373 | + type = array_index_nospec(type, maxtype + 1); |
|---|
| 80 | 374 | pt = &policy[type]; |
|---|
| 81 | 375 | |
|---|
| 82 | 376 | BUG_ON(pt->type > NLA_TYPE_MAX); |
|---|
| .. | .. |
|---|
| 84 | 378 | if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) { |
|---|
| 85 | 379 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
|---|
| 86 | 380 | current->comm, type); |
|---|
| 381 | + if (validate & NL_VALIDATE_STRICT_ATTRS) { |
|---|
| 382 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 383 | + "invalid attribute length"); |
|---|
| 384 | + return -EINVAL; |
|---|
| 385 | + } |
|---|
| 386 | + } |
|---|
| 387 | + |
|---|
| 388 | + if (validate & NL_VALIDATE_NESTED) { |
|---|
| 389 | + if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) && |
|---|
| 390 | + !(nla->nla_type & NLA_F_NESTED)) { |
|---|
| 391 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 392 | + "NLA_F_NESTED is missing"); |
|---|
| 393 | + return -EINVAL; |
|---|
| 394 | + } |
|---|
| 395 | + if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY && |
|---|
| 396 | + pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) { |
|---|
| 397 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 398 | + "NLA_F_NESTED not expected"); |
|---|
| 399 | + return -EINVAL; |
|---|
| 400 | + } |
|---|
| 87 | 401 | } |
|---|
| 88 | 402 | |
|---|
| 89 | 403 | switch (pt->type) { |
|---|
| 404 | + case NLA_REJECT: |
|---|
| 405 | + if (extack && pt->reject_message) { |
|---|
| 406 | + NL_SET_BAD_ATTR(extack, nla); |
|---|
| 407 | + extack->_msg = pt->reject_message; |
|---|
| 408 | + return -EINVAL; |
|---|
| 409 | + } |
|---|
| 410 | + err = -EINVAL; |
|---|
| 411 | + goto out_err; |
|---|
| 412 | + |
|---|
| 90 | 413 | case NLA_FLAG: |
|---|
| 91 | 414 | if (attrlen > 0) |
|---|
| 92 | | - return -ERANGE; |
|---|
| 415 | + goto out_err; |
|---|
| 93 | 416 | break; |
|---|
| 94 | 417 | |
|---|
| 95 | 418 | case NLA_BITFIELD32: |
|---|
| 96 | 419 | if (attrlen != sizeof(struct nla_bitfield32)) |
|---|
| 97 | | - return -ERANGE; |
|---|
| 420 | + goto out_err; |
|---|
| 98 | 421 | |
|---|
| 99 | | - return validate_nla_bitfield32(nla, pt->validation_data); |
|---|
| 422 | + err = validate_nla_bitfield32(nla, pt->bitfield32_valid); |
|---|
| 423 | + if (err) |
|---|
| 424 | + goto out_err; |
|---|
| 425 | + break; |
|---|
| 100 | 426 | |
|---|
| 101 | 427 | case NLA_NUL_STRING: |
|---|
| 102 | 428 | if (pt->len) |
|---|
| .. | .. |
|---|
| 104 | 430 | else |
|---|
| 105 | 431 | minlen = attrlen; |
|---|
| 106 | 432 | |
|---|
| 107 | | - if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) |
|---|
| 108 | | - return -EINVAL; |
|---|
| 433 | + if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
|---|
| 434 | + err = -EINVAL; |
|---|
| 435 | + goto out_err; |
|---|
| 436 | + } |
|---|
| 109 | 437 | /* fall through */ |
|---|
| 110 | 438 | |
|---|
| 111 | 439 | case NLA_STRING: |
|---|
| 112 | 440 | if (attrlen < 1) |
|---|
| 113 | | - return -ERANGE; |
|---|
| 441 | + goto out_err; |
|---|
| 114 | 442 | |
|---|
| 115 | 443 | if (pt->len) { |
|---|
| 116 | 444 | char *buf = nla_data(nla); |
|---|
| .. | .. |
|---|
| 119 | 447 | attrlen--; |
|---|
| 120 | 448 | |
|---|
| 121 | 449 | if (attrlen > pt->len) |
|---|
| 122 | | - return -ERANGE; |
|---|
| 450 | + goto out_err; |
|---|
| 123 | 451 | } |
|---|
| 124 | 452 | break; |
|---|
| 125 | 453 | |
|---|
| 126 | 454 | case NLA_BINARY: |
|---|
| 127 | 455 | if (pt->len && attrlen > pt->len) |
|---|
| 128 | | - return -ERANGE; |
|---|
| 456 | + goto out_err; |
|---|
| 129 | 457 | break; |
|---|
| 130 | 458 | |
|---|
| 131 | | - case NLA_NESTED_COMPAT: |
|---|
| 132 | | - if (attrlen < pt->len) |
|---|
| 133 | | - return -ERANGE; |
|---|
| 134 | | - if (attrlen < NLA_ALIGN(pt->len)) |
|---|
| 135 | | - break; |
|---|
| 136 | | - if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN) |
|---|
| 137 | | - return -ERANGE; |
|---|
| 138 | | - nla = nla_data(nla) + NLA_ALIGN(pt->len); |
|---|
| 139 | | - if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla)) |
|---|
| 140 | | - return -ERANGE; |
|---|
| 141 | | - break; |
|---|
| 142 | 459 | case NLA_NESTED: |
|---|
| 143 | 460 | /* a nested attributes is allowed to be empty; if its not, |
|---|
| 144 | 461 | * it must have a size of at least NLA_HDRLEN. |
|---|
| 145 | 462 | */ |
|---|
| 146 | 463 | if (attrlen == 0) |
|---|
| 147 | 464 | break; |
|---|
| 465 | + if (attrlen < NLA_HDRLEN) |
|---|
| 466 | + goto out_err; |
|---|
| 467 | + if (pt->nested_policy) { |
|---|
| 468 | + err = __nla_validate_parse(nla_data(nla), nla_len(nla), |
|---|
| 469 | + pt->len, pt->nested_policy, |
|---|
| 470 | + validate, extack, NULL, |
|---|
| 471 | + depth + 1); |
|---|
| 472 | + if (err < 0) { |
|---|
| 473 | + /* |
|---|
| 474 | + * return directly to preserve the inner |
|---|
| 475 | + * error message/attribute pointer |
|---|
| 476 | + */ |
|---|
| 477 | + return err; |
|---|
| 478 | + } |
|---|
| 479 | + } |
|---|
| 480 | + break; |
|---|
| 481 | + case NLA_NESTED_ARRAY: |
|---|
| 482 | + /* a nested array attribute is allowed to be empty; if its not, |
|---|
| 483 | + * it must have a size of at least NLA_HDRLEN. |
|---|
| 484 | + */ |
|---|
| 485 | + if (attrlen == 0) |
|---|
| 486 | + break; |
|---|
| 487 | + if (attrlen < NLA_HDRLEN) |
|---|
| 488 | + goto out_err; |
|---|
| 489 | + if (pt->nested_policy) { |
|---|
| 490 | + int err; |
|---|
| 491 | + |
|---|
| 492 | + err = nla_validate_array(nla_data(nla), nla_len(nla), |
|---|
| 493 | + pt->len, pt->nested_policy, |
|---|
| 494 | + extack, validate, depth); |
|---|
| 495 | + if (err < 0) { |
|---|
| 496 | + /* |
|---|
| 497 | + * return directly to preserve the inner |
|---|
| 498 | + * error message/attribute pointer |
|---|
| 499 | + */ |
|---|
| 500 | + return err; |
|---|
| 501 | + } |
|---|
| 502 | + } |
|---|
| 503 | + break; |
|---|
| 504 | + |
|---|
| 505 | + case NLA_UNSPEC: |
|---|
| 506 | + if (validate & NL_VALIDATE_UNSPEC) { |
|---|
| 507 | + NL_SET_ERR_MSG_ATTR(extack, nla, |
|---|
| 508 | + "Unsupported attribute"); |
|---|
| 509 | + return -EINVAL; |
|---|
| 510 | + } |
|---|
| 511 | + if (attrlen < pt->len) |
|---|
| 512 | + goto out_err; |
|---|
| 513 | + break; |
|---|
| 514 | + |
|---|
| 148 | 515 | default: |
|---|
| 149 | 516 | if (pt->len) |
|---|
| 150 | 517 | minlen = pt->len; |
|---|
| 151 | | - else if (pt->type != NLA_UNSPEC) |
|---|
| 518 | + else |
|---|
| 152 | 519 | minlen = nla_attr_minlen[pt->type]; |
|---|
| 153 | 520 | |
|---|
| 154 | 521 | if (attrlen < minlen) |
|---|
| 155 | | - return -ERANGE; |
|---|
| 522 | + goto out_err; |
|---|
| 523 | + } |
|---|
| 524 | + |
|---|
| 525 | + /* further validation */ |
|---|
| 526 | + switch (pt->validation_type) { |
|---|
| 527 | + case NLA_VALIDATE_NONE: |
|---|
| 528 | + /* nothing to do */ |
|---|
| 529 | + break; |
|---|
| 530 | + case NLA_VALIDATE_RANGE_PTR: |
|---|
| 531 | + case NLA_VALIDATE_RANGE: |
|---|
| 532 | + case NLA_VALIDATE_RANGE_WARN_TOO_LONG: |
|---|
| 533 | + case NLA_VALIDATE_MIN: |
|---|
| 534 | + case NLA_VALIDATE_MAX: |
|---|
| 535 | + err = nla_validate_int_range(pt, nla, extack, validate); |
|---|
| 536 | + if (err) |
|---|
| 537 | + return err; |
|---|
| 538 | + break; |
|---|
| 539 | + case NLA_VALIDATE_MASK: |
|---|
| 540 | + err = nla_validate_mask(pt, nla, extack); |
|---|
| 541 | + if (err) |
|---|
| 542 | + return err; |
|---|
| 543 | + break; |
|---|
| 544 | + case NLA_VALIDATE_FUNCTION: |
|---|
| 545 | + if (pt->validate) { |
|---|
| 546 | + err = pt->validate(nla, extack); |
|---|
| 547 | + if (err) |
|---|
| 548 | + return err; |
|---|
| 549 | + } |
|---|
| 550 | + break; |
|---|
| 551 | + } |
|---|
| 552 | + |
|---|
| 553 | + return 0; |
|---|
| 554 | +out_err: |
|---|
| 555 | + NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt, |
|---|
| 556 | + "Attribute failed policy validation"); |
|---|
| 557 | + return err; |
|---|
| 558 | +} |
|---|
| 559 | + |
|---|
| 560 | +static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
|---|
| 561 | + const struct nla_policy *policy, |
|---|
| 562 | + unsigned int validate, |
|---|
| 563 | + struct netlink_ext_ack *extack, |
|---|
| 564 | + struct nlattr **tb, unsigned int depth) |
|---|
| 565 | +{ |
|---|
| 566 | + const struct nlattr *nla; |
|---|
| 567 | + int rem; |
|---|
| 568 | + |
|---|
| 569 | + if (depth >= MAX_POLICY_RECURSION_DEPTH) { |
|---|
| 570 | + NL_SET_ERR_MSG(extack, |
|---|
| 571 | + "allowed policy recursion depth exceeded"); |
|---|
| 572 | + return -EINVAL; |
|---|
| 573 | + } |
|---|
| 574 | + |
|---|
| 575 | + if (tb) |
|---|
| 576 | + memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
|---|
| 577 | + |
|---|
| 578 | + nla_for_each_attr(nla, head, len, rem) { |
|---|
| 579 | + u16 type = nla_type(nla); |
|---|
| 580 | + |
|---|
| 581 | + if (type == 0 || type > maxtype) { |
|---|
| 582 | + if (validate & NL_VALIDATE_MAXTYPE) { |
|---|
| 583 | + NL_SET_ERR_MSG_ATTR(extack, nla, |
|---|
| 584 | + "Unknown attribute type"); |
|---|
| 585 | + return -EINVAL; |
|---|
| 586 | + } |
|---|
| 587 | + continue; |
|---|
| 588 | + } |
|---|
| 589 | + type = array_index_nospec(type, maxtype + 1); |
|---|
| 590 | + if (policy) { |
|---|
| 591 | + int err = validate_nla(nla, maxtype, policy, |
|---|
| 592 | + validate, extack, depth); |
|---|
| 593 | + |
|---|
| 594 | + if (err < 0) |
|---|
| 595 | + return err; |
|---|
| 596 | + } |
|---|
| 597 | + |
|---|
| 598 | + if (tb) |
|---|
| 599 | + tb[type] = (struct nlattr *)nla; |
|---|
| 600 | + } |
|---|
| 601 | + |
|---|
| 602 | + if (unlikely(rem > 0)) { |
|---|
| 603 | + pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
|---|
| 604 | + rem, current->comm); |
|---|
| 605 | + NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes"); |
|---|
| 606 | + if (validate & NL_VALIDATE_TRAILING) |
|---|
| 607 | + return -EINVAL; |
|---|
| 156 | 608 | } |
|---|
| 157 | 609 | |
|---|
| 158 | 610 | return 0; |
|---|
| 159 | 611 | } |
|---|
| 160 | 612 | |
|---|
| 161 | 613 | /** |
|---|
| 162 | | - * nla_validate - Validate a stream of attributes |
|---|
| 614 | + * __nla_validate - Validate a stream of attributes |
|---|
| 163 | 615 | * @head: head of attribute stream |
|---|
| 164 | 616 | * @len: length of attribute stream |
|---|
| 165 | 617 | * @maxtype: maximum attribute type to be expected |
|---|
| 166 | 618 | * @policy: validation policy |
|---|
| 619 | + * @validate: validation strictness |
|---|
| 167 | 620 | * @extack: extended ACK report struct |
|---|
| 168 | 621 | * |
|---|
| 169 | 622 | * Validates all attributes in the specified attribute stream against the |
|---|
| 170 | | - * specified policy. Attributes with a type exceeding maxtype will be |
|---|
| 171 | | - * ignored. See documenation of struct nla_policy for more details. |
|---|
| 623 | + * specified policy. Validation depends on the validate flags passed, see |
|---|
| 624 | + * &enum netlink_validation for more details on that. |
|---|
| 625 | + * See documenation of struct nla_policy for more details. |
|---|
| 172 | 626 | * |
|---|
| 173 | 627 | * Returns 0 on success or a negative error code. |
|---|
| 174 | 628 | */ |
|---|
| 175 | | -int nla_validate(const struct nlattr *head, int len, int maxtype, |
|---|
| 176 | | - const struct nla_policy *policy, |
|---|
| 177 | | - struct netlink_ext_ack *extack) |
|---|
| 629 | +int __nla_validate(const struct nlattr *head, int len, int maxtype, |
|---|
| 630 | + const struct nla_policy *policy, unsigned int validate, |
|---|
| 631 | + struct netlink_ext_ack *extack) |
|---|
| 178 | 632 | { |
|---|
| 179 | | - const struct nlattr *nla; |
|---|
| 180 | | - int rem; |
|---|
| 181 | | - |
|---|
| 182 | | - nla_for_each_attr(nla, head, len, rem) { |
|---|
| 183 | | - int err = validate_nla(nla, maxtype, policy); |
|---|
| 184 | | - |
|---|
| 185 | | - if (err < 0) { |
|---|
| 186 | | - if (extack) |
|---|
| 187 | | - extack->bad_attr = nla; |
|---|
| 188 | | - return err; |
|---|
| 189 | | - } |
|---|
| 190 | | - } |
|---|
| 191 | | - |
|---|
| 192 | | - return 0; |
|---|
| 633 | + return __nla_validate_parse(head, len, maxtype, policy, validate, |
|---|
| 634 | + extack, NULL, 0); |
|---|
| 193 | 635 | } |
|---|
| 194 | | -EXPORT_SYMBOL(nla_validate); |
|---|
| 636 | +EXPORT_SYMBOL(__nla_validate); |
|---|
| 195 | 637 | |
|---|
| 196 | 638 | /** |
|---|
| 197 | 639 | * nla_policy_len - Determin the max. length of a policy |
|---|
| .. | .. |
|---|
| 223 | 665 | EXPORT_SYMBOL(nla_policy_len); |
|---|
| 224 | 666 | |
|---|
| 225 | 667 | /** |
|---|
| 226 | | - * nla_parse - Parse a stream of attributes into a tb buffer |
|---|
| 668 | + * __nla_parse - Parse a stream of attributes into a tb buffer |
|---|
| 227 | 669 | * @tb: destination array with maxtype+1 elements |
|---|
| 228 | 670 | * @maxtype: maximum attribute type to be expected |
|---|
| 229 | 671 | * @head: head of attribute stream |
|---|
| 230 | 672 | * @len: length of attribute stream |
|---|
| 231 | 673 | * @policy: validation policy |
|---|
| 674 | + * @validate: validation strictness |
|---|
| 675 | + * @extack: extended ACK pointer |
|---|
| 232 | 676 | * |
|---|
| 233 | 677 | * Parses a stream of attributes and stores a pointer to each attribute in |
|---|
| 234 | | - * the tb array accessible via the attribute type. Attributes with a type |
|---|
| 235 | | - * exceeding maxtype will be silently ignored for backwards compatibility |
|---|
| 236 | | - * reasons. policy may be set to NULL if no validation is required. |
|---|
| 678 | + * the tb array accessible via the attribute type. |
|---|
| 679 | + * Validation is controlled by the @validate parameter. |
|---|
| 237 | 680 | * |
|---|
| 238 | 681 | * Returns 0 on success or a negative error code. |
|---|
| 239 | 682 | */ |
|---|
| 240 | | -int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
|---|
| 241 | | - int len, const struct nla_policy *policy, |
|---|
| 242 | | - struct netlink_ext_ack *extack) |
|---|
| 683 | +int __nla_parse(struct nlattr **tb, int maxtype, |
|---|
| 684 | + const struct nlattr *head, int len, |
|---|
| 685 | + const struct nla_policy *policy, unsigned int validate, |
|---|
| 686 | + struct netlink_ext_ack *extack) |
|---|
| 243 | 687 | { |
|---|
| 244 | | - const struct nlattr *nla; |
|---|
| 245 | | - int rem, err; |
|---|
| 246 | | - |
|---|
| 247 | | - memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
|---|
| 248 | | - |
|---|
| 249 | | - nla_for_each_attr(nla, head, len, rem) { |
|---|
| 250 | | - u16 type = nla_type(nla); |
|---|
| 251 | | - |
|---|
| 252 | | - if (type > 0 && type <= maxtype) { |
|---|
| 253 | | - if (policy) { |
|---|
| 254 | | - err = validate_nla(nla, maxtype, policy); |
|---|
| 255 | | - if (err < 0) { |
|---|
| 256 | | - NL_SET_ERR_MSG_ATTR(extack, nla, |
|---|
| 257 | | - "Attribute failed policy validation"); |
|---|
| 258 | | - goto errout; |
|---|
| 259 | | - } |
|---|
| 260 | | - } |
|---|
| 261 | | - |
|---|
| 262 | | - tb[type] = (struct nlattr *)nla; |
|---|
| 263 | | - } |
|---|
| 264 | | - } |
|---|
| 265 | | - |
|---|
| 266 | | - if (unlikely(rem > 0)) |
|---|
| 267 | | - pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
|---|
| 268 | | - rem, current->comm); |
|---|
| 269 | | - |
|---|
| 270 | | - err = 0; |
|---|
| 271 | | -errout: |
|---|
| 272 | | - return err; |
|---|
| 688 | + return __nla_validate_parse(head, len, maxtype, policy, validate, |
|---|
| 689 | + extack, tb, 0); |
|---|
| 273 | 690 | } |
|---|
| 274 | | -EXPORT_SYMBOL(nla_parse); |
|---|
| 691 | +EXPORT_SYMBOL(__nla_parse); |
|---|
| 275 | 692 | |
|---|
| 276 | 693 | /** |
|---|
| 277 | 694 | * nla_find - Find a specific attribute in a stream of attributes |
|---|
| .. | .. |
|---|
| 457 | 874 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, |
|---|
| 458 | 875 | int attrlen, int padattr) |
|---|
| 459 | 876 | { |
|---|
| 460 | | - if (nla_need_padding_for_64bit(skb)) |
|---|
| 461 | | - nla_align_64bit(skb, padattr); |
|---|
| 877 | + nla_align_64bit(skb, padattr); |
|---|
| 462 | 878 | |
|---|
| 463 | 879 | return __nla_reserve(skb, attrtype, attrlen); |
|---|
| 464 | 880 | } |
|---|