ipv6: fix ioam6_id_wide sysctl on 32-bit architectures
Subsystems (2)
PATCH 1/1 ipv6: fix ioam6_id_wide sysctl on 32-bit architectures
net.ipv6.ioam6_id_wide stores a u64 (netns_sysctl_ipv6.ioam6_id_wide), but its table entry uses proc_doulongvec_minmax() with .maxlen = sizeof(u64) and .extra2 pointing at a u64. On 32-bit, unsigned long is 4 bytes, so the handler treats the field as a vector of two 32-bit values and reads the u64 max through an unsigned long pointer:
- reading prints two numbers ("4294967295 16777215" by default on little-endian);
- writing one value only replaces one half of the u64: on little-endian "echo 5 >" leaves 0x00ffffff00000005; on big-endian it sets the upper half, and the range check uses the upper word of ioam6_id_wide_max (0x00ffffff).
The wide namespace ID emitted in IOAM trace data is then not the configured value. Same class of issue as commit 162cf592b577 ("net/smc: fix limit_smc_hs sysctl on big-endian architecture").
There is no u64 variant of proc_doulongvec_minmax(), so use a small local handler that goes through proc_dostring() and kstrtou64(). A generic u64 helper in kernel/sysctl.c would be a larger change for net and stable; it can follow separately. For normal input the handler behaves as the 64-bit one does today: base 0 parsing (decimal, hex or octal), surrounding white space is accepted, and values above IOAM6_DEFAULT_ID_WIDE return -EINVAL. Input too long for the 24-byte buffer is rejected rather than truncated by proc_dostring().
Found by reading the code with LLM assistance. Tested on i386 and x86_64 under QEMU: on i386 the sysctl now reads and writes a single 64-bit value, and on x86_64 reads, writes and range checks behave as before.
---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).
Based on net.git main 2a324a58a084. Built for x86_64, i386 and 32-bit powerpc (pmac32_defconfig); W=1 shows no warnings for sysctl_net_ipv6.c before or after.
Runtime tested on i386 and x86_64 under QEMU (TCG), with a small static test program as init: default read, echo 5, the maximum, max + 1, values above U64_MAX, -1, non-numeric, hex, white space, over-long input, two-part writes with sysctl_writes_strict 1 and 0, and a new netns. Before this change i386 read "4294967295 16777215", and after "echo 5" it read "5 16777215". With it, i386 gives the same results as x86_64. On x86_64 two corner cases change: a write of only "\n" is now a no-op instead of -EINVAL, and a number with 21 to 23 digits (only possible with leading zeros) is now accepted.
I did not add Cc: stable; please add it if you think it fits.
net/ipv6/sysctl_net_ipv6.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-)
net/ipv6/sysctl_net_ipv6.c +34 -2
@@ -30,6 +30,39 @@ static u32 rt6_multipath_hash_fields_all_mask =static u32 ioam6_id_max =IOAM6_DEFAULT_ID ;static u64 ioam6_id_wide_max =IOAM6_DEFAULT_ID_WIDE ; +/* unsigned long is 32-bit on some arches, so handle the u64 here */ +static int proc_ioam6_id_wide (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +u64 *valp = table->data; +char buf[24 ] ="" ; +const struct ctl_table tmp = { + .data = buf, + .maxlen =sizeof (buf), + }; +u64 val; +int ret; + +if (!write) { +snprintf (buf,sizeof (buf),"%llu" ,READ_ONCE (*valp)); +return proc_dostring (&tmp, write, buffer, lenp, ppos); + } + +/* proc_dostring() would silently truncate a longer input */ +if (*lenp >=sizeof (buf)) +return -EINVAL ; + + ret =proc_dostring (&tmp, write, buffer, lenp, ppos); +if (ret || !buf[0 ]) +return ret; + +if (kstrtou64 (strim (buf),0 , &val) || val > ioam6_id_wide_max) +return -EINVAL ; + +WRITE_ONCE (*valp, val); +return 0 ; +} +static int proc_rt6_multipath_hash_policy (const struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) {
@@ -210,8 +243,7 @@ static const struct ctl_table ipv6_table_template[] = { .data = &init_net.ipv6.sysctl.ioam6_id_wide, .maxlen = sizeof(u64), .mode = 0644, - .proc_handler = proc_doulongvec_minmax, - .extra2 = &ioam6_id_wide_max, + .proc_handler = proc_ioam6_id_wide, }, };
Signature · 1 line
2.43.0
Quoted 101 lines · Shashank Mohan Jain
net.ipv6.ioam6_id_wide stores a u64 (netns_sysctl_ipv6.ioam6_id_wide), but its table entry uses
proc_doulongvec_minmax()with .maxlen = sizeof(u64) and .extra2 pointing at a u64. On 32-bit, unsigned long is 4 bytes, so the handler treats the field as a vector of two 32-bit values and reads the u64 max through an unsigned long pointer:- reading prints two numbers ("4294967295 16777215" by default on
little-endian); - writing one value only replaces one half of the u64: on little-endian "echo 5 >" leaves 0x00ffffff00000005; on big-endian it sets the upper half, and the range check uses the upper word of ioam6_id_wide_max (0x00ffffff).The wide namespace ID emitted in IOAM trace data is then not the configured value. Same class of issue as commit
162cf592b577("net/smc: fixlimit_smc_hssysctl on big-endian architecture").There is no u64 variant of
proc_doulongvec_minmax(), so use a small local handler that goes throughproc_dostring()andkstrtou64(). A generic u64 helper inkernel/sysctl.cwould be a larger change for net and stable; it can follow separately. For normal input the handler behaves as the 64-bit one does today: base 0 parsing (decimal, hex or octal), surrounding white space is accepted, and values aboveIOAM6_DEFAULT_ID_WIDEreturn-EINVAL. Input too long for the 24-byte buffer is rejected rather than truncated byproc_dostring().Found by reading the code with LLM assistance. Tested on i386 and
x86_64under QEMU: on i386 the sysctl now reads and writes a single 64-bit value, and onx86_64reads, writes and range checks behave as before.Fixes 9ee11f0fff20 ("ipv6: ioam: Data plane support for Pre-allocated Trace")Assisted-by LLMSigned-off-by Shashank Mohan Jain <[email protected]>---
Prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).Based on net.git main
2a324a58a084. Built forx86_64, i386 and 32-bit powerpc (pmac32_defconfig); W=1 shows no warnings forsysctl_net_ipv6.cbefore or after.Runtime tested on i386 and
x86_64under QEMU (TCG), with a small static test program as init: default read, echo 5, the maximum, max + 1, values aboveU64_MAX, -1, non-numeric, hex, white space, over-long input, two-part writes withsysctl_writes_strict1 and 0, and a new netns. Before this change i386 read "4294967295 16777215", and after "echo 5" it read "5 16777215". With it, i386 gives the same results asx86_64. Onx86_64two corner cases change: a write of only "\n" is now a no-op instead of-EINVAL, and a number with 21 to 23 digits (only possible with leading zeros) is now accepted.I did not add Cc: stable; please add it if you think it fits.
net/ipv6/sysctl_net_ipv6.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-)net/ipv6/sysctl_net_ipv6.c +33
-0@@ -30,6 +30,39 @@ static u32 rt6_multipath_hash_fields_all_mask =static u32 ioam6_id_max =IOAM6_DEFAULT_ID ;static u64 ioam6_id_wide_max =IOAM6_DEFAULT_ID_WIDE ; +/* unsigned long is 32-bit on some arches, so handle the u64 here */ +static int proc_ioam6_id_wide (const struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +u64 *valp = table->data; +char buf[24 ] ="" ; +const struct ctl_table tmp = { + .data = buf, + .maxlen =sizeof (buf), + }; +u64 val; +int ret; + +if (!write) { +snprintf (buf,sizeof (buf),"%llu" ,READ_ONCE (*valp)); +return proc_dostring (&tmp, write, buffer, lenp, ppos); + } + +/* proc_dostring() would silently truncate a longer input */ +if (*lenp >=sizeof (buf)) +return -EINVAL ; + + ret =proc_dostring (&tmp, write, buffer, lenp, ppos); +if (ret || !buf[0 ]) +return ret; + +if (kstrtou64 (strim (buf),0 , &val) || val > ioam6_id_wide_max) +return -EINVAL ; + +WRITE_ONCE (*valp, val); +return 0 ; +} +static int proc_rt6_multipath_hash_policy (const struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) {
net/ipv6/sysctl_net_ipv6.c static const struct ctl_table ipv6_table_template[] = { · line 249
Quoted 9 lines
net/ipv6/sysctl_net_ipv6.c +1
-2@@ -210,8 +243,7 @@ static const struct ctl_table ipv6_table_template[] = { .data = &init_net.ipv6.sysctl.ioam6_id_wide, .maxlen = sizeof(u64), .mode = 0644, - .proc_handler = proc_doulongvec_minmax, - .extra2 = &ioam6_id_wide_max, + .proc_handler = proc_ioam6_id_wide, }, };
Thanks for this patch. I agree that a generic u64 helper could wait for net-next. Please keep -EINVAL for a lone "\n", as before. For example, not tested, save *ppos and return pos ? 0 : -EINVAL when buf is empty, so that strict-mode continued writes are still ignored. Also worth a line in the changelog: on 64-bit, "5 6" (for example) is now rejected with -EINVAL (which is good) instead of being a partial write that sets "5" (which was bad).
For v2: make sure the above is addressed and all maintainers are CC'ed (Joel Granados is missing). Please also fix your Assisted-by tag.
pw-bot: cr