Skip to contentSkip to conversation

netlink: Return unsigned value for nla_len()

PATCH Superseded Kees Cook 2 participants 4 messages Dec 2, 2023 – Dec 5, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 netlink: Return unsigned value for nla_len()

The return value from nla_len() is never expected to be negative, and can never be more than struct nlattr::nla_len (a u16). Adjust the prototype on the function. This will let GCC's value range optimization passes know that the return can never be negative, and can never be larger than u16. As recently discussed[1], this silences the following warning in GCC 12+:

net/wireless/nl80211.c: In function 'nl80211_set_cqm_rssi.isra':
net/wireless/nl80211.c:12892:17: warning: 'memcpy' specified bound 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]

12892 |                 memcpy(cqm_config->rssi_thresholds, thresholds,
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12893 |                        flex_array_size(cqm_config, rssi_thresholds,
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12894 |                                        n_thresholds));
      |                                        ~~~~~~~~~~~~~~

A future change would be to clamp the subtraction to make sure it never wraps around if nla_len is somehow less than NLA_HDRLEN, which would have the additional benefit of being defensive in the face of nlattr corruption or logic errors.

---
v2:
- do not clamp return value (kuba)
- adjust NLA_HDRLEN to be u16 also
v1: https://lore.kernel.org/all/[email protected]/

---
 include/net/netlink.h        | 2 +-
 include/uapi/linux/netlink.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/net/netlink.h +1 -1
index 83bdf787aeee..7678a596a86b 100644
@@ -1200,7 +1200,7 @@ static inline void *nla_data(const struct nlattr *nla)
  * nla_len - length of payload
  * @nla: netlink attribute
  */
-static inline int nla_len(const struct nlattr *nla)
+static inline u16 nla_len(const struct nlattr *nla)
 {
 	return nla->nla_len - NLA_HDRLEN;
 }
include/uapi/linux/netlink.h +1 -1
index f87aaf28a649..270feed9fd63 100644
@@ -247,7 +247,7 @@ struct nlattr {
 
 #define NLA_ALIGNTO		4
 #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
-#define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
+#define NLA_HDRLEN		((__u16) NLA_ALIGN(sizeof(struct nlattr)))
 
 /* Generic 32 bitflags attribute content sent to the kernel.
  *
Signature · 1 line
2.34.1
#1 lore raw

Le 02/12/2023 à 21:25, Kees Cook a écrit :

Quoted 62 lines

The return value from nla_len() is never expected to be negative, and can never be more than struct nlattr::nla_len (a u16). Adjust the prototype on the function. This will let GCC's value range optimization passes know that the return can never be negative, and can never be larger than u16. As recently discussed[1], this silences the following warning in GCC 12+:

net/wireless/nl80211.c: In function 'nl80211_set_cqm_rssi.isra':
net/wireless/nl80211.c:12892:17: warning: 'memcpy' specified bound 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]

12892 |                 memcpy(cqm_config->rssi_thresholds, thresholds,
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12893 |                        flex_array_size(cqm_config, rssi_thresholds,
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12894 |                                        n_thresholds));
      |                                        ~~~~~~~~~~~~~~

A future change would be to clamp the subtraction to make sure it never wraps around if nla_len is somehow less than NLA_HDRLEN, which would have the additional benefit of being defensive in the face of nlattr corruption or logic errors.

---
v2:
- do not clamp return value (kuba)
- adjust NLA_HDRLEN to be u16 also
v1: https://lore.kernel.org/all/[email protected]/

---
 include/net/netlink.h        | 2 +-
 include/uapi/linux/netlink.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/net/netlink.h +1 -1
index 83bdf787aeee..7678a596a86b 100644
@@ -1200,7 +1200,7 @@ static inline void *nla_data(const struct nlattr *nla)
  * nla_len - length of payload
  * @nla: netlink attribute
  */
-static inline int nla_len(const struct nlattr *nla)
+static inline u16 nla_len(const struct nlattr *nla)
 {
 	return nla->nla_len - NLA_HDRLEN;
 }
include/uapi/linux/netlink.h +0 -0
index f87aaf28a649..270feed9fd63 100644

include/uapi/linux/netlink.h struct nlattr { · line 250

Quoted 5 lines
include/uapi/linux/netlink.h +1 -1
@@ -247,7 +247,7 @@ struct nlattr {
 
 #define NLA_ALIGNTO		4
 #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
-#define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
+#define NLA_HDRLEN		((__u16) NLA_ALIGN(sizeof(struct nlattr)))

I wonder if this may break the compilation of some userspace tools with errors like comparing signed and unsigned values.

Regards,
Nicolas

author
#2 lore raw
Quoted 64 lines · Nicolas Dichtel

Le 02/12/2023 à 21:25, Kees Cook a écrit :

Quoted 63 lines

The return value from nla_len() is never expected to be negative, and can never be more than struct nlattr::nla_len (a u16). Adjust the prototype on the function. This will let GCC's value range optimization passes know that the return can never be negative, and can never be larger than u16. As recently discussed[1], this silences the following warning in GCC 12+:

net/wireless/nl80211.c: In function 'nl80211_set_cqm_rssi.isra':
net/wireless/nl80211.c:12892:17: warning: 'memcpy' specified bound 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]

12892 |                 memcpy(cqm_config->rssi_thresholds, thresholds,
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12893 |                        flex_array_size(cqm_config, rssi_thresholds,
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12894 |                                        n_thresholds));
      |                                        ~~~~~~~~~~~~~~

A future change would be to clamp the subtraction to make sure it never wraps around if nla_len is somehow less than NLA_HDRLEN, which would have the additional benefit of being defensive in the face of nlattr corruption or logic errors.

---
v2:
- do not clamp return value (kuba)
- adjust NLA_HDRLEN to be u16 also
v1: https://lore.kernel.org/all/[email protected]/

---
 include/net/netlink.h        | 2 +-
 include/uapi/linux/netlink.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/net/netlink.h +1 -1
index 83bdf787aeee..7678a596a86b 100644
@@ -1200,7 +1200,7 @@ static inline void *nla_data(const struct nlattr *nla)
  * nla_len - length of payload
  * @nla: netlink attribute
  */
-static inline int nla_len(const struct nlattr *nla)
+static inline u16 nla_len(const struct nlattr *nla)
 {
 	return nla->nla_len - NLA_HDRLEN;
 }
include/uapi/linux/netlink.h +0 -0
index f87aaf28a649..270feed9fd63 100644
@@ -247,7 +247,7 @@ struct nlattr {
 
Quoted 6 lines

include/uapi/linux/netlink.h struct nlattr { · line 250

Quoted 4 lines
include/uapi/linux/netlink.h +1 -1
 #define NLA_ALIGNTO		4
 #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
-#define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
+#define NLA_HDRLEN		((__u16) NLA_ALIGN(sizeof(struct nlattr)))

I wonder if this may break the compilation of some userspace tools with errors like comparing signed and unsigned values.

Should I drop this part, then?

Signature · 1 line
Kees Cook
#3 lore raw

Le 04/12/2023 à 23:21, Kees Cook a écrit :
[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/uapi/linux/netlink.h +0 -0
index f87aaf28a649..270feed9fd63 100644
@@ -247,7 +247,7 @@ struct nlattr {
 
Quoted 9 lines
Quoted 6 lines

include/uapi/linux/netlink.h struct nlattr { · line 250

Quoted 4 lines
include/uapi/linux/netlink.h +1 -1
 #define NLA_ALIGNTO		4
 #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
-#define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
+#define NLA_HDRLEN		((__u16) NLA_ALIGN(sizeof(struct nlattr)))

I wonder if this may break the compilation of some userspace tools with errors like comparing signed and unsigned values.

Should I drop this part, then?

Yes please.

Thank you,
Nicolas