Skip to content

Commit 30bed99

Browse files
JustinStittkees
authored andcommitted
um: vector: refactor deprecated strncpy
`strncpy` is deprecated for use on NUL-terminated destination strings [1]. A suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on its destination buffer argument which is _not_ the case for `strncpy`! In this case, we are able to drop the now superfluous `... - 1` instances because `strscpy` will automatically truncate the last byte by setting it to a NUL byte if the source size exceeds the destination size or if the source string is not NUL-terminated. I've also opted to remove the seemingly useless char* casts. I'm not sure why they're present at all since (after expanding the `ifr_name` macro) `ifr.ifr_ifrn.ifrn_name` is a char* already. All in all, `strscpy` is a more robust and less ambiguous interface while also letting us remove some `... -1`'s which cleans things up a bit. [1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [2]: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html Link: KSPP#90 Cc: [email protected] Signed-off-by: Justin Stitt <[email protected]> Acked-by: Anton Ivanov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 967afdf commit 30bed99

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

arch/um/drivers/vector_user.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static int create_tap_fd(char *iface)
141141
}
142142
memset(&ifr, 0, sizeof(ifr));
143143
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
144-
strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
144+
strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
145145

146146
err = ioctl(fd, TUNSETIFF, (void *) &ifr);
147147
if (err != 0) {
@@ -171,7 +171,7 @@ static int create_raw_fd(char *iface, int flags, int proto)
171171
goto raw_fd_cleanup;
172172
}
173173
memset(&ifr, 0, sizeof(ifr));
174-
strncpy((char *)&ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
174+
strscpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
175175
if (ioctl(fd, SIOCGIFINDEX, (void *) &ifr) < 0) {
176176
err = -errno;
177177
goto raw_fd_cleanup;

0 commit comments

Comments
 (0)