1 // SPDX-License-Identifier: GPL-2.0
3 * NETLINK Netlink attributes
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/jiffies.h>
13 #include <linux/nospec.h>
14 #include <linux/skbuff.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <net/netlink.h>
19 /* For these data types, attribute length should be exactly the given
20 * size. However, to maintain compatibility with broken commands, if the
21 * attribute length does not match the expected size a warning is emitted
22 * to the user that the command is sending invalid data and needs to be fixed.
24 static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
25 [NLA_U8] = sizeof(u8),
26 [NLA_U16] = sizeof(u16),
27 [NLA_U32] = sizeof(u32),
28 [NLA_U64] = sizeof(u64),
29 [NLA_S8] = sizeof(s8),
30 [NLA_S16] = sizeof(s16),
31 [NLA_S32] = sizeof(s32),
32 [NLA_S64] = sizeof(s64),
35 static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
36 [NLA_U8] = sizeof(u8),
37 [NLA_U16] = sizeof(u16),
38 [NLA_U32] = sizeof(u32),
39 [NLA_U64] = sizeof(u64),
40 [NLA_MSECS] = sizeof(u64),
41 [NLA_NESTED] = NLA_HDRLEN,
42 [NLA_S8] = sizeof(s8),
43 [NLA_S16] = sizeof(s16),
44 [NLA_S32] = sizeof(s32),
45 [NLA_S64] = sizeof(s64),
48 static int validate_nla_bitfield32(const struct nlattr *nla,
49 const u32 *valid_flags_mask)
51 const struct nla_bitfield32 *bf = nla_data(nla);
53 if (!valid_flags_mask)
56 /*disallow invalid bit selector */
57 if (bf->selector & ~*valid_flags_mask)
60 /*disallow invalid bit values */
61 if (bf->value & ~*valid_flags_mask)
64 /*disallow valid bit values that are not selected*/
65 if (bf->value & ~bf->selector)
71 static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
72 const struct nla_policy *policy,
73 struct netlink_ext_ack *extack,
74 unsigned int validate)
76 const struct nlattr *entry;
79 nla_for_each_attr(entry, head, len, rem) {
82 if (nla_len(entry) == 0)
85 if (nla_len(entry) < NLA_HDRLEN) {
86 NL_SET_ERR_MSG_ATTR(extack, entry,
87 "Array element too short");
91 ret = __nla_validate(nla_data(entry), nla_len(entry),
92 maxtype, policy, validate, extack);
100 static int nla_validate_int_range(const struct nla_policy *pt,
101 const struct nlattr *nla,
102 struct netlink_ext_ack *extack)
104 bool validate_min, validate_max;
107 validate_min = pt->validation_type == NLA_VALIDATE_RANGE ||
108 pt->validation_type == NLA_VALIDATE_MIN;
109 validate_max = pt->validation_type == NLA_VALIDATE_RANGE ||
110 pt->validation_type == NLA_VALIDATE_MAX;
114 value = nla_get_u8(nla);
117 value = nla_get_u16(nla);
120 value = nla_get_u32(nla);
123 value = nla_get_s8(nla);
126 value = nla_get_s16(nla);
129 value = nla_get_s32(nla);
132 value = nla_get_s64(nla);
135 /* treat this one specially, since it may not fit into s64 */
136 if ((validate_min && nla_get_u64(nla) < pt->min) ||
137 (validate_max && nla_get_u64(nla) > pt->max)) {
138 NL_SET_ERR_MSG_ATTR(extack, nla,
139 "integer out of range");
148 if ((validate_min && value < pt->min) ||
149 (validate_max && value > pt->max)) {
150 NL_SET_ERR_MSG_ATTR(extack, nla,
151 "integer out of range");
158 static int validate_nla(const struct nlattr *nla, int maxtype,
159 const struct nla_policy *policy, unsigned int validate,
160 struct netlink_ext_ack *extack)
162 u16 strict_start_type = policy[0].strict_start_type;
163 const struct nla_policy *pt;
164 int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
167 if (strict_start_type && type >= strict_start_type)
168 validate |= NL_VALIDATE_STRICT;
170 if (type <= 0 || type > maxtype)
173 type = array_index_nospec(type, maxtype + 1);
176 BUG_ON(pt->type > NLA_TYPE_MAX);
178 if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) ||
179 (pt->type == NLA_EXACT_LEN_WARN && attrlen != pt->len)) {
180 pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
181 current->comm, type);
182 if (validate & NL_VALIDATE_STRICT_ATTRS) {
183 NL_SET_ERR_MSG_ATTR(extack, nla,
184 "invalid attribute length");
189 if (validate & NL_VALIDATE_NESTED) {
190 if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
191 !(nla->nla_type & NLA_F_NESTED)) {
192 NL_SET_ERR_MSG_ATTR(extack, nla,
193 "NLA_F_NESTED is missing");
196 if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
197 pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
198 NL_SET_ERR_MSG_ATTR(extack, nla,
199 "NLA_F_NESTED not expected");
206 if (attrlen != pt->len)
211 if (extack && pt->validation_data) {
212 NL_SET_BAD_ATTR(extack, nla);
213 extack->_msg = pt->validation_data;
225 if (attrlen != sizeof(struct nla_bitfield32))
228 err = validate_nla_bitfield32(nla, pt->validation_data);
235 minlen = min_t(int, attrlen, pt->len + 1);
239 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
250 char *buf = nla_data(nla);
252 if (buf[attrlen - 1] == '\0')
255 if (attrlen > pt->len)
261 if (pt->len && attrlen > pt->len)
266 /* a nested attributes is allowed to be empty; if its not,
267 * it must have a size of at least NLA_HDRLEN.
271 if (attrlen < NLA_HDRLEN)
273 if (pt->validation_data) {
274 err = __nla_validate(nla_data(nla), nla_len(nla), pt->len,
275 pt->validation_data, validate,
279 * return directly to preserve the inner
280 * error message/attribute pointer
286 case NLA_NESTED_ARRAY:
287 /* a nested array attribute is allowed to be empty; if its not,
288 * it must have a size of at least NLA_HDRLEN.
292 if (attrlen < NLA_HDRLEN)
294 if (pt->validation_data) {
297 err = nla_validate_array(nla_data(nla), nla_len(nla),
298 pt->len, pt->validation_data,
302 * return directly to preserve the inner
303 * error message/attribute pointer
311 if (validate & NL_VALIDATE_UNSPEC) {
312 NL_SET_ERR_MSG_ATTR(extack, nla,
313 "Unsupported attribute");
318 if (attrlen < pt->len)
326 minlen = nla_attr_minlen[pt->type];
328 if (attrlen < minlen)
332 /* further validation */
333 switch (pt->validation_type) {
334 case NLA_VALIDATE_NONE:
337 case NLA_VALIDATE_RANGE:
338 case NLA_VALIDATE_MIN:
339 case NLA_VALIDATE_MAX:
340 err = nla_validate_int_range(pt, nla, extack);
344 case NLA_VALIDATE_FUNCTION:
346 err = pt->validate(nla, extack);
355 NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation");
359 static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
360 const struct nla_policy *policy,
361 unsigned int validate,
362 struct netlink_ext_ack *extack,
365 const struct nlattr *nla;
369 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
371 nla_for_each_attr(nla, head, len, rem) {
372 u16 type = nla_type(nla);
374 if (type == 0 || type > maxtype) {
375 if (validate & NL_VALIDATE_MAXTYPE) {
376 NL_SET_ERR_MSG_ATTR(extack, nla,
377 "Unknown attribute type");
382 type = array_index_nospec(type, maxtype + 1);
384 int err = validate_nla(nla, maxtype, policy,
392 tb[type] = (struct nlattr *)nla;
395 if (unlikely(rem > 0)) {
396 pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
398 NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
399 if (validate & NL_VALIDATE_TRAILING)
407 * __nla_validate - Validate a stream of attributes
408 * @head: head of attribute stream
409 * @len: length of attribute stream
410 * @maxtype: maximum attribute type to be expected
411 * @policy: validation policy
412 * @validate: validation strictness
413 * @extack: extended ACK report struct
415 * Validates all attributes in the specified attribute stream against the
416 * specified policy. Validation depends on the validate flags passed, see
417 * &enum netlink_validation for more details on that.
418 * See documenation of struct nla_policy for more details.
420 * Returns 0 on success or a negative error code.
422 int __nla_validate(const struct nlattr *head, int len, int maxtype,
423 const struct nla_policy *policy, unsigned int validate,
424 struct netlink_ext_ack *extack)
426 return __nla_validate_parse(head, len, maxtype, policy, validate,
429 EXPORT_SYMBOL(__nla_validate);
432 * nla_policy_len - Determin the max. length of a policy
433 * @policy: policy to use
434 * @n: number of policies
436 * Determines the max. length of the policy. It is currently used
437 * to allocated Netlink buffers roughly the size of the actual
440 * Returns 0 on success or a negative error code.
443 nla_policy_len(const struct nla_policy *p, int n)
447 for (i = 0; i < n; i++, p++) {
449 len += nla_total_size(p->len);
450 else if (nla_attr_len[p->type])
451 len += nla_total_size(nla_attr_len[p->type]);
452 else if (nla_attr_minlen[p->type])
453 len += nla_total_size(nla_attr_minlen[p->type]);
458 EXPORT_SYMBOL(nla_policy_len);
461 * __nla_parse - Parse a stream of attributes into a tb buffer
462 * @tb: destination array with maxtype+1 elements
463 * @maxtype: maximum attribute type to be expected
464 * @head: head of attribute stream
465 * @len: length of attribute stream
466 * @policy: validation policy
467 * @validate: validation strictness
468 * @extack: extended ACK pointer
470 * Parses a stream of attributes and stores a pointer to each attribute in
471 * the tb array accessible via the attribute type.
472 * Validation is controlled by the @validate parameter.
474 * Returns 0 on success or a negative error code.
476 int __nla_parse(struct nlattr **tb, int maxtype,
477 const struct nlattr *head, int len,
478 const struct nla_policy *policy, unsigned int validate,
479 struct netlink_ext_ack *extack)
481 return __nla_validate_parse(head, len, maxtype, policy, validate,
484 EXPORT_SYMBOL(__nla_parse);
487 * nla_find - Find a specific attribute in a stream of attributes
488 * @head: head of attribute stream
489 * @len: length of attribute stream
490 * @attrtype: type of attribute to look for
492 * Returns the first attribute in the stream matching the specified type.
494 struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
496 const struct nlattr *nla;
499 nla_for_each_attr(nla, head, len, rem)
500 if (nla_type(nla) == attrtype)
501 return (struct nlattr *)nla;
505 EXPORT_SYMBOL(nla_find);
508 * nla_strlcpy - Copy string attribute payload into a sized buffer
509 * @dst: where to copy the string to
510 * @nla: attribute to copy the string from
511 * @dstsize: size of destination buffer
513 * Copies at most dstsize - 1 bytes into the destination buffer.
514 * The result is always a valid NUL-terminated string. Unlike
515 * strlcpy the destination buffer is always padded out.
517 * Returns the length of the source buffer.
519 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
521 size_t srclen = nla_len(nla);
522 char *src = nla_data(nla);
524 if (srclen > 0 && src[srclen - 1] == '\0')
528 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
530 memset(dst, 0, dstsize);
531 memcpy(dst, src, len);
536 EXPORT_SYMBOL(nla_strlcpy);
539 * nla_strdup - Copy string attribute payload into a newly allocated buffer
540 * @nla: attribute to copy the string from
541 * @flags: the type of memory to allocate (see kmalloc).
543 * Returns a pointer to the allocated buffer or NULL on error.
545 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
547 size_t srclen = nla_len(nla);
548 char *src = nla_data(nla), *dst;
550 if (srclen > 0 && src[srclen - 1] == '\0')
553 dst = kmalloc(srclen + 1, flags);
555 memcpy(dst, src, srclen);
560 EXPORT_SYMBOL(nla_strdup);
563 * nla_memcpy - Copy a netlink attribute into another memory area
564 * @dest: where to copy to memcpy
565 * @src: netlink attribute to copy from
566 * @count: size of the destination area
568 * Note: The number of bytes copied is limited by the length of
569 * attribute's payload. memcpy
571 * Returns the number of bytes copied.
573 int nla_memcpy(void *dest, const struct nlattr *src, int count)
575 int minlen = min_t(int, count, nla_len(src));
577 memcpy(dest, nla_data(src), minlen);
579 memset(dest + minlen, 0, count - minlen);
583 EXPORT_SYMBOL(nla_memcpy);
586 * nla_memcmp - Compare an attribute with sized memory area
587 * @nla: netlink attribute
589 * @size: size of memory area
591 int nla_memcmp(const struct nlattr *nla, const void *data,
594 int d = nla_len(nla) - size;
597 d = memcmp(nla_data(nla), data, size);
601 EXPORT_SYMBOL(nla_memcmp);
604 * nla_strcmp - Compare a string attribute against a string
605 * @nla: netlink string attribute
606 * @str: another string
608 int nla_strcmp(const struct nlattr *nla, const char *str)
610 int len = strlen(str);
611 char *buf = nla_data(nla);
612 int attrlen = nla_len(nla);
615 while (attrlen > 0 && buf[attrlen - 1] == '\0')
620 d = memcmp(nla_data(nla), str, len);
624 EXPORT_SYMBOL(nla_strcmp);
628 * __nla_reserve - reserve room for attribute on the skb
629 * @skb: socket buffer to reserve room on
630 * @attrtype: attribute type
631 * @attrlen: length of attribute payload
633 * Adds a netlink attribute header to a socket buffer and reserves
634 * room for the payload but does not copy it.
636 * The caller is responsible to ensure that the skb provides enough
637 * tailroom for the attribute header and payload.
639 struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
643 nla = skb_put(skb, nla_total_size(attrlen));
644 nla->nla_type = attrtype;
645 nla->nla_len = nla_attr_size(attrlen);
647 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
651 EXPORT_SYMBOL(__nla_reserve);
654 * __nla_reserve_64bit - reserve room for attribute on the skb and align it
655 * @skb: socket buffer to reserve room on
656 * @attrtype: attribute type
657 * @attrlen: length of attribute payload
658 * @padattr: attribute type for the padding
660 * Adds a netlink attribute header to a socket buffer and reserves
661 * room for the payload but does not copy it. It also ensure that this
662 * attribute will have a 64-bit aligned nla_data() area.
664 * The caller is responsible to ensure that the skb provides enough
665 * tailroom for the attribute header and payload.
667 struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
668 int attrlen, int padattr)
670 if (nla_need_padding_for_64bit(skb))
671 nla_align_64bit(skb, padattr);
673 return __nla_reserve(skb, attrtype, attrlen);
675 EXPORT_SYMBOL(__nla_reserve_64bit);
678 * __nla_reserve_nohdr - reserve room for attribute without header
679 * @skb: socket buffer to reserve room on
680 * @attrlen: length of attribute payload
682 * Reserves room for attribute payload without a header.
684 * The caller is responsible to ensure that the skb provides enough
685 * tailroom for the payload.
687 void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
689 return skb_put_zero(skb, NLA_ALIGN(attrlen));
691 EXPORT_SYMBOL(__nla_reserve_nohdr);
694 * nla_reserve - reserve room for attribute on the skb
695 * @skb: socket buffer to reserve room on
696 * @attrtype: attribute type
697 * @attrlen: length of attribute payload
699 * Adds a netlink attribute header to a socket buffer and reserves
700 * room for the payload but does not copy it.
702 * Returns NULL if the tailroom of the skb is insufficient to store
703 * the attribute header and payload.
705 struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
707 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
710 return __nla_reserve(skb, attrtype, attrlen);
712 EXPORT_SYMBOL(nla_reserve);
715 * nla_reserve_64bit - reserve room for attribute on the skb and align it
716 * @skb: socket buffer to reserve room on
717 * @attrtype: attribute type
718 * @attrlen: length of attribute payload
719 * @padattr: attribute type for the padding
721 * Adds a netlink attribute header to a socket buffer and reserves
722 * room for the payload but does not copy it. It also ensure that this
723 * attribute will have a 64-bit aligned nla_data() area.
725 * Returns NULL if the tailroom of the skb is insufficient to store
726 * the attribute header and payload.
728 struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
733 if (nla_need_padding_for_64bit(skb))
734 len = nla_total_size_64bit(attrlen);
736 len = nla_total_size(attrlen);
737 if (unlikely(skb_tailroom(skb) < len))
740 return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
742 EXPORT_SYMBOL(nla_reserve_64bit);
745 * nla_reserve_nohdr - reserve room for attribute without header
746 * @skb: socket buffer to reserve room on
747 * @attrlen: length of attribute payload
749 * Reserves room for attribute payload without a header.
751 * Returns NULL if the tailroom of the skb is insufficient to store
752 * the attribute payload.
754 void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
756 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
759 return __nla_reserve_nohdr(skb, attrlen);
761 EXPORT_SYMBOL(nla_reserve_nohdr);
764 * __nla_put - Add a netlink attribute to a socket buffer
765 * @skb: socket buffer to add attribute to
766 * @attrtype: attribute type
767 * @attrlen: length of attribute payload
768 * @data: head of attribute payload
770 * The caller is responsible to ensure that the skb provides enough
771 * tailroom for the attribute header and payload.
773 void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
778 nla = __nla_reserve(skb, attrtype, attrlen);
779 memcpy(nla_data(nla), data, attrlen);
781 EXPORT_SYMBOL(__nla_put);
784 * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
785 * @skb: socket buffer to add attribute to
786 * @attrtype: attribute type
787 * @attrlen: length of attribute payload
788 * @data: head of attribute payload
789 * @padattr: attribute type for the padding
791 * The caller is responsible to ensure that the skb provides enough
792 * tailroom for the attribute header and payload.
794 void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
795 const void *data, int padattr)
799 nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
800 memcpy(nla_data(nla), data, attrlen);
802 EXPORT_SYMBOL(__nla_put_64bit);
805 * __nla_put_nohdr - Add a netlink attribute without header
806 * @skb: socket buffer to add attribute to
807 * @attrlen: length of attribute payload
808 * @data: head of attribute payload
810 * The caller is responsible to ensure that the skb provides enough
811 * tailroom for the attribute payload.
813 void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
817 start = __nla_reserve_nohdr(skb, attrlen);
818 memcpy(start, data, attrlen);
820 EXPORT_SYMBOL(__nla_put_nohdr);
823 * nla_put - Add a netlink attribute to a socket buffer
824 * @skb: socket buffer to add attribute to
825 * @attrtype: attribute type
826 * @attrlen: length of attribute payload
827 * @data: head of attribute payload
829 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
830 * the attribute header and payload.
832 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
834 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
837 __nla_put(skb, attrtype, attrlen, data);
840 EXPORT_SYMBOL(nla_put);
843 * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
844 * @skb: socket buffer to add attribute to
845 * @attrtype: attribute type
846 * @attrlen: length of attribute payload
847 * @data: head of attribute payload
848 * @padattr: attribute type for the padding
850 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
851 * the attribute header and payload.
853 int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
854 const void *data, int padattr)
858 if (nla_need_padding_for_64bit(skb))
859 len = nla_total_size_64bit(attrlen);
861 len = nla_total_size(attrlen);
862 if (unlikely(skb_tailroom(skb) < len))
865 __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
868 EXPORT_SYMBOL(nla_put_64bit);
871 * nla_put_nohdr - Add a netlink attribute without header
872 * @skb: socket buffer to add attribute to
873 * @attrlen: length of attribute payload
874 * @data: head of attribute payload
876 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
877 * the attribute payload.
879 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
881 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
884 __nla_put_nohdr(skb, attrlen, data);
887 EXPORT_SYMBOL(nla_put_nohdr);
890 * nla_append - Add a netlink attribute without header or padding
891 * @skb: socket buffer to add attribute to
892 * @attrlen: length of attribute payload
893 * @data: head of attribute payload
895 * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
896 * the attribute payload.
898 int nla_append(struct sk_buff *skb, int attrlen, const void *data)
900 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
903 skb_put_data(skb, data, attrlen);
906 EXPORT_SYMBOL(nla_append);