Re: [PATCH net-next v2] af_unix: Refine UNIX pathname sockets autobind identifier length

From: Kuniyuki Iwashima
Date: Thu Feb 06 2025 - 03:59:11 EST


From: Liang Jie <buaajxlj@xxxxxxx>
Date: Thu, 6 Feb 2025 16:19:05 +0800
> Hi Kuniyuki,
>
> The logs from 'netdev/build_allmodconfig_warn' is as follows:
> ../net/unix/af_unix.c: In function ‘unix_autobind’:
> ../net/unix/af_unix.c:1222:52: warning: ‘snprintf’ output truncated before the last format character [-Wformat-truncation=]
> 1222 | snprintf(addr->name->sun_path + 1, 5, "%05x", ordernum);
> | ^
> ../net/unix/af_unix.c:1222:9: note: ‘snprintf’ output 6 bytes into a destination of size 5
> 1222 | snprintf(addr->name->sun_path + 1, 5, "%05x", ordernum);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> snprintf() also append a trailing '\0' at the end of the sun_path.

I didn't say snprintf() would work rather we need a variant of it that
does not terminate string with \0.


>
> Now, I think of three options. Which one do you think we should choose?
>
> 1. Allocate an additional byte during the kzalloc phase.
> addr = kzalloc(sizeof(*addr) + offsetof(struct sockaddr_un, sun_path) +
> UNIX_AUTOBIND_LEN + 1, GFP_KERNEL);
>
> 2. Use temp buffer and memcpy() for handling.
>
> 3. Keep the current code as it is.
>
> Do you have any other suggestions?

I'd choose 3. as said in v1 thread. We can't avoid hard-coding and
adjustment like +1 and -1 here.