Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/landlock: NULL-terminate unix pathname addresses

The size of Unix pathname addresses is computed in selftests using
offsetof(struct sockaddr_un, sun_path) + strlen(xxx). It should have
been that +1, which makes addresses passed to the libc and kernel
non-NULL-terminated. unix_mkname_bsd() fixes that in Linux so there is
no harm, but just using sizeof(the address struct) should improve
readability.

Signed-off-by: Matthieu Buffet <matthieu@buffet.re>
Reviewed-by: Günther Noack <gnoack@google.com>
Link: https://lore.kernel.org/r/20251202215141.689986-1-matthieu@buffet.re
Signed-off-by: Mickaël Salaün <mic@digikod.net>

authored by

Matthieu Buffet and committed by
Mickaël Salaün
e4aa4461 e1a57c33

+20 -25
+11 -13
tools/testing/selftests/landlock/fs_test.c
··· 4362 4362 { 4363 4363 const char *const path = file1_s1d1; 4364 4364 int srv_fd, cli_fd, ruleset_fd; 4365 - socklen_t size; 4366 - struct sockaddr_un srv_un, cli_un; 4365 + struct sockaddr_un srv_un = { 4366 + .sun_family = AF_UNIX, 4367 + }; 4368 + struct sockaddr_un cli_un = { 4369 + .sun_family = AF_UNIX, 4370 + }; 4367 4371 const struct landlock_ruleset_attr attr = { 4368 4372 .handled_access_fs = LANDLOCK_ACCESS_FS_IOCTL_DEV, 4369 4373 }; 4370 4374 4371 4375 /* Sets up a server */ 4372 - srv_un.sun_family = AF_UNIX; 4373 - strncpy(srv_un.sun_path, path, sizeof(srv_un.sun_path)); 4374 - 4375 4376 ASSERT_EQ(0, unlink(path)); 4376 4377 srv_fd = socket(AF_UNIX, SOCK_STREAM, 0); 4377 4378 ASSERT_LE(0, srv_fd); 4378 4379 4379 - size = offsetof(struct sockaddr_un, sun_path) + strlen(srv_un.sun_path); 4380 - ASSERT_EQ(0, bind(srv_fd, (struct sockaddr *)&srv_un, size)); 4380 + strncpy(srv_un.sun_path, path, sizeof(srv_un.sun_path)); 4381 + ASSERT_EQ(0, bind(srv_fd, (struct sockaddr *)&srv_un, sizeof(srv_un))); 4382 + 4381 4383 ASSERT_EQ(0, listen(srv_fd, 10 /* qlen */)); 4382 4384 4383 4385 /* Enables Landlock. */ ··· 4389 4387 ASSERT_EQ(0, close(ruleset_fd)); 4390 4388 4391 4389 /* Sets up a client connection to it */ 4392 - cli_un.sun_family = AF_UNIX; 4393 4390 cli_fd = socket(AF_UNIX, SOCK_STREAM, 0); 4394 4391 ASSERT_LE(0, cli_fd); 4395 4392 4396 - bzero(&cli_un, sizeof(cli_un)); 4397 - cli_un.sun_family = AF_UNIX; 4398 4393 strncpy(cli_un.sun_path, path, sizeof(cli_un.sun_path)); 4399 - size = offsetof(struct sockaddr_un, sun_path) + strlen(cli_un.sun_path); 4400 - 4401 - ASSERT_EQ(0, connect(cli_fd, (struct sockaddr *)&cli_un, size)); 4394 + ASSERT_EQ(0, 4395 + connect(cli_fd, (struct sockaddr *)&cli_un, sizeof(cli_un))); 4402 4396 4403 4397 /* FIONREAD and other IOCTLs should not be forbidden. */ 4404 4398 EXPECT_EQ(0, test_fionread_ioctl(cli_fd));
+9 -12
tools/testing/selftests/landlock/scoped_abstract_unix_test.c
··· 779 779 780 780 TEST_F(various_address_sockets, scoped_pathname_sockets) 781 781 { 782 - socklen_t size_stream, size_dgram; 783 782 pid_t child; 784 783 int status; 785 784 char buf_child, buf_parent; ··· 797 798 /* Pathname address. */ 798 799 snprintf(stream_pathname_addr.sun_path, 799 800 sizeof(stream_pathname_addr.sun_path), "%s", stream_path); 800 - size_stream = offsetof(struct sockaddr_un, sun_path) + 801 - strlen(stream_pathname_addr.sun_path); 802 801 snprintf(dgram_pathname_addr.sun_path, 803 802 sizeof(dgram_pathname_addr.sun_path), "%s", dgram_path); 804 - size_dgram = offsetof(struct sockaddr_un, sun_path) + 805 - strlen(dgram_pathname_addr.sun_path); 806 803 807 804 /* Abstract address. */ 808 805 memset(&stream_abstract_addr, 0, sizeof(stream_abstract_addr)); ··· 836 841 /* Connects with pathname sockets. */ 837 842 stream_pathname_socket = socket(AF_UNIX, SOCK_STREAM, 0); 838 843 ASSERT_LE(0, stream_pathname_socket); 839 - ASSERT_EQ(0, connect(stream_pathname_socket, 840 - &stream_pathname_addr, size_stream)); 844 + ASSERT_EQ(0, 845 + connect(stream_pathname_socket, &stream_pathname_addr, 846 + sizeof(stream_pathname_addr))); 841 847 ASSERT_EQ(1, write(stream_pathname_socket, "b", 1)); 842 848 EXPECT_EQ(0, close(stream_pathname_socket)); 843 849 ··· 846 850 dgram_pathname_socket = socket(AF_UNIX, SOCK_DGRAM, 0); 847 851 ASSERT_LE(0, dgram_pathname_socket); 848 852 err = sendto(dgram_pathname_socket, "c", 1, 0, 849 - &dgram_pathname_addr, size_dgram); 853 + &dgram_pathname_addr, sizeof(dgram_pathname_addr)); 850 854 EXPECT_EQ(1, err); 851 855 852 856 /* Sends with connection. */ 853 - ASSERT_EQ(0, connect(dgram_pathname_socket, 854 - &dgram_pathname_addr, size_dgram)); 857 + ASSERT_EQ(0, 858 + connect(dgram_pathname_socket, &dgram_pathname_addr, 859 + sizeof(dgram_pathname_addr))); 855 860 ASSERT_EQ(1, write(dgram_pathname_socket, "d", 1)); 856 861 EXPECT_EQ(0, close(dgram_pathname_socket)); 857 862 ··· 907 910 stream_pathname_socket = socket(AF_UNIX, SOCK_STREAM, 0); 908 911 ASSERT_LE(0, stream_pathname_socket); 909 912 ASSERT_EQ(0, bind(stream_pathname_socket, &stream_pathname_addr, 910 - size_stream)); 913 + sizeof(stream_pathname_addr))); 911 914 ASSERT_EQ(0, listen(stream_pathname_socket, backlog)); 912 915 913 916 dgram_pathname_socket = socket(AF_UNIX, SOCK_DGRAM, 0); 914 917 ASSERT_LE(0, dgram_pathname_socket); 915 918 ASSERT_EQ(0, bind(dgram_pathname_socket, &dgram_pathname_addr, 916 - size_dgram)); 919 + sizeof(dgram_pathname_addr))); 917 920 918 921 /* Sets up abstract servers. */ 919 922 stream_abstract_socket = socket(AF_UNIX, SOCK_STREAM, 0);