Skip to contentSkip to conversation

keys, dns: drop unused upayload->data NUL terminator

PATCH Superseded Thorsten Blum 3 participants 8 messages Apr 9 – Apr 15

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 keys, dns: drop unused upayload->data NUL terminator

In dns_resolver_preparse(), do not NUL-terminate ->data and allocate one byte less. The NUL terminator is never used and only ->datalen bytes are accessed.

---
No changes in patch 1/2.

---
 net/dns_resolver/dns_key.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/dns_resolver/dns_key.c +1 -2
index c3c8c3240ef9..451247864a63 100644
@@ -203,7 +203,7 @@ dns_resolver_preparse(struct key_preparsed_payload *prep)
 	kdebug("store result");
 	prep->quotalen = result_len;
 
-	upayload = kmalloc_flex(*upayload, data, result_len + 1);
+	upayload = kmalloc_flex(*upayload, data, result_len);
 	if (!upayload) {
 		kleave(" = -ENOMEM");
 		return -ENOMEM;
@@ -211,7 +211,6 @@ dns_resolver_preparse(struct key_preparsed_payload *prep)
 
 	upayload->datalen = result_len;
 	memcpy(upayload->data, data, result_len);
-	upayload->data[result_len] = '\0';
 
 	prep->payload.data[dns_key_data] = upayload;
 	kleave(" = 0");
#1 lore raw

PATCH v2 2/2 KEYS: annotate struct user_key_payload with __counted_by

Add the __counted_by() compiler attribute to the flexible array member 'data' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
Changes in v2:
- Use __aligned(8) as suggested by David
- v1: https://lore.kernel.org/lkml/[email protected]/

---
 include/keys/user-type.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/keys/user-type.h +2 -1
index 386c31432789..c29ed9f5d300 100644
@@ -27,7 +27,8 @@
 struct user_key_payload {
 	struct rcu_head	rcu;		/* RCU destructor */
 	unsigned short	datalen;	/* length of this data */
-	char		data[] __aligned(__alignof__(u64)); /* actual data */
+	char		data[]		/* actual data */
+			__aligned(8) __counted_by(datalen);
 };
 
 extern struct key_type key_type_user;
maintainer
#3 lore raw
In reply to Thorsten Blum
Quoted 3 lines · On Fri, 10 Apr 2026 00:57:02 +0200 Thorsten Blum wrote:

In dns_resolver_preparse(), do not NUL-terminate ->data and allocate one byte less. The NUL terminator is never used and only ->datalen bytes are accessed.

I can't see where this is used at all.
Please write better commit messages, there's no way this 1 byte is worth the amount of time I wasted trying to review this :/

#4 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 10 Apr 2026 00:57:02 +0200 Thorsten Blum wrote:

In dns_resolver_preparse(), do not NUL-terminate ->data and allocate one byte less. The NUL terminator is never used and only ->datalen bytes are accessed.

I can't see where this is used at all.
Please write better commit messages, there's no way this 1 byte is worth the amount of time I wasted trying to review this :/

The point of patch 1/2 is not the removed NUL terminator itself, but to prepare for patch 2/2, which adds __counted_by() and requires ->datalen to match the number of elements in ->data.

Currently, that is not the case because ->data includes an extra NUL despite never being used as a C string. Removing the unused terminator makes the length match the allocation size and allows adding the __counted_by() annotation.

I can fold this into the __counted_by() patch if you prefer.

maintainer
#5 lore raw
Quoted 14 lines · On Mon, 13 Apr 2026 01:04:54 +0200 Thorsten Blum wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 10 Apr 2026 00:57:02 +0200 Thorsten Blum wrote:

In dns_resolver_preparse(), do not NUL-terminate ->data and allocate one byte less. The NUL terminator is never used and only ->datalen bytes are accessed.

I can't see where this is used at all.
Please write better commit messages, there's no way this 1 byte is worth the amount of time I wasted trying to review this :/

The point of patch 1/2 is not the removed NUL terminator itself, but to prepare for patch 2/2, which adds __counted_by() and requires ->datalen to match the number of elements in ->data.

Quoted 6 lines

Currently, that is not the case because ->data includes an extra NUL despite never being used as a C string. Removing the unused terminator makes the length match the allocation size and allows adding the __counted_by() annotation.

I can fold this into the __counted_by() patch if you prefer.

I understand that part, but I don't get where the data from which the terminating character is removed, is used. Only other access I saw was freeing it, the rest of the callback seem to looking at the error, not the data..

#6 lore raw
Quoted 20 lines · Jakub Kicinski
Quoted 19 lines · On Mon, 13 Apr 2026 01:04:54 +0200 Thorsten Blum wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 10 Apr 2026 00:57:02 +0200 Thorsten Blum wrote:

In dns_resolver_preparse(), do not NUL-terminate ->data and allocate one byte less. The NUL terminator is never used and only ->datalen bytes are accessed.

I can't see where this is used at all.
Please write better commit messages, there's no way this 1 byte is worth the amount of time I wasted trying to review this :/

The point of patch 1/2 is not the removed NUL terminator itself, but to prepare for patch 2/2, which adds __counted_by() and requires ->datalen to match the number of elements in ->data.

Currently, that is not the case because ->data includes an extra NUL despite never being used as a C string. Removing the unused terminator makes the length match the allocation size and allows adding the __counted_by() annotation.

Quoted 6 lines
Quoted 1 line

I can fold this into the __counted_by() patch if you prefer.

I understand that part, but I don't get where the data from which the terminating character is removed, is used. Only other access I saw was freeing it, the rest of the callback seem to looking at the error, not the data..

->data and ->datalen are used in multiple places.

For example, in dns_query() in net/dns_resolver/dns_query.c:

	upayload = user_key_payload_locked(rkey);
	len = upayload->datalen;

	if (_result) {
		ret = -ENOMEM;
		*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
		if (!*_result)
			goto put;
	}

In cifs_set_cifscreds() in fs/smb/client/connect.c:

	/* find first : in payload */
	payload = upayload->data;
	delim = strnchr(payload, upayload->datalen, ':');
maintainer
#7 lore raw
Quoted 37 lines · On Mon, 13 Apr 2026 02:31:46 +0200 Thorsten Blum wrote:
Quoted 2 lines · Jakub Kicinski
Quoted 1 line · On Mon, 13 Apr 2026 01:04:54 +0200 Thorsten Blum wrote:

On Sun, Apr 12, 2026 at 02:10:04PM -0700, Jakub Kicinski wrote:

[...]
[...]
[...]

Quoted 10 lines
Quoted 10 lines

The point of patch 1/2 is not the removed NUL terminator itself, but to prepare for patch 2/2, which adds __counted_by() and requires ->datalen to match the number of elements in ->data.

Currently, that is not the case because ->data includes an extra NUL despite never being used as a C string. Removing the unused terminator makes the length match the allocation size and allows adding the __counted_by() annotation.

Quoted 6 lines
Quoted 1 line

I can fold this into the __counted_by() patch if you prefer.

I understand that part, but I don't get where the data from which the terminating character is removed, is used. Only other access I saw was freeing it, the rest of the callback seem to looking at the error, not the data..

->data and ->datalen are used in multiple places.

For example, in dns_query() in net/dns_resolver/dns_query.c:

	upayload = user_key_payload_locked(rkey);
	len = upayload->datalen;

	if (_result) {
		ret = -ENOMEM;
		*_result = kmemdup_nul(upayload->data, len, GFP_KERNEL);
		if (!*_result)
			goto put;
	}
Quoted 6 lines

In cifs_set_cifscreds() in fs/smb/client/connect.c:

	/* find first : in payload */
	payload = upayload->data;
	delim = strnchr(payload, upayload->datalen, ':');

Alright, could you repost this after the merge window and CC David and Jarkko on both patches? They supposedly maintain this.

maintainer
#2 lore raw

Re: [PATCH net-next v2 2/2] KEYS: annotate struct user_key_payload with __counted_by

In reply to Thorsten Blum
Quoted 20 lines · Thorsten Blum

Add the __counted_by() compiler attribute to the flexible array member 'data' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
Changes in v2:
- Use __aligned(8) as suggested by David
- v1: https://lore.kernel.org/lkml/[email protected]/

---
 include/keys/user-type.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/keys/user-type.h +0 -0
index 386c31432789..c29ed9f5d300 100644

include/keys/user-type.h line 34

Quoted 9 lines
include/keys/user-type.h +2 -1
@@ -27,7 +27,8 @@
 struct user_key_payload {
 	struct rcu_head	rcu;		/* RCU destructor */
 	unsigned short	datalen;	/* length of this data */
-	char		data[] __aligned(__alignof__(u64)); /* actual data */
+	char		data[]		/* actual data */
+			__aligned(8) __counted_by(datalen);
 };
 
 extern struct key_type key_type_user;

The change is acceptable but I see no point split the declaration into two lines (as long as it passes checkpatch.pl as per line length).

BR, Jarkko