Increase bind() converage - GH#538
cover Increase bind() converage - GH#538
This patchset updates syscalls/bind02 to new API and adds a test that bind() opens sockets for incoming connections. The test is split into two programs, one for stream-oriented sockets and the other for datagram-oriented sockets.
Martin Doucha (2):
Update syscalls/bind02 to new API
Add connection tests for bind()
runtest/syscalls | 2 + testcases/kernel/syscalls/bind/.gitignore | 2 + testcases/kernel/syscalls/bind/Makefile | 5 + testcases/kernel/syscalls/bind/bind02.c | 153 ++++----------- testcases/kernel/syscalls/bind/bind04.c | 206 ++++++++++++++++++++ testcases/kernel/syscalls/bind/bind05.c | 219 ++++++++++++++++++++++ testcases/kernel/syscalls/bind/libbind.c | 47 +++++ testcases/kernel/syscalls/bind/libbind.h | 15 ++ 8 files changed, 537 insertions(+), 112 deletions(-) create mode 100644 testcases/kernel/syscalls/bind/bind04.c create mode 100644 testcases/kernel/syscalls/bind/bind05.c create mode 100644 testcases/kernel/syscalls/bind/libbind.c create mode 100644 testcases/kernel/syscalls/bind/libbind.h
Signature · 1 line
2.22.1
PATCH 1/2 Update syscalls/bind02 to new API
--- testcases/kernel/syscalls/bind/bind02.c | 153 +++++++----------------- 1 file changed, 41 insertions(+), 112 deletions(-)
testcases/kernel/syscalls/bind/bind02.c +41 -112
@@ -1,146 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* * Copyright (c) International Business Machines Corp., 2004 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* 07/2004 Written by Dan Jones +* 07/2004 Ported to LTP format by Robbie Williamson +* Copyright (c) 2019 Martin Doucha <[email protected]> */ /* -* Test Name: bind02 -* * Test Description: -* Make sure bind() gives EACCESS error for (non-root) users. -* -* Usage: <for command-line> -* bind01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* -* HISTORY -* 07/2004 Written by Dan Jones -* 07/2004 Ported to LTP format by Robbie Williamson -* -* RESTRICTIONS: -* None. -* +* Make sure bind() of privileged port gives EACCESS error for non-root users. */ -#include <stdio.h> -#include <stdlib.h> +#include <string.h> #include <unistd.h> -#include <errno.h> -#include <fcntl.h> #include <pwd.h> #include <grp.h> #include <sys/types.h> #include <sys/socket.h> -#include <sys/un.h> -#include <netinet/in.h> -#include "test.h" - -char *TCID ="bind02" ; -int testno; -int TST_TOTAL =1 ; - -/* This port needs to be a Privledged port */ -#define TCP_PRIVLEGED_COM_PORT 463 +#include "tst_test.h" -struct passwd *pw; -struct group *gr; +/* This port needs to be a privileged port */ +#define TCP_PRIVILEGED_PORT 463 +#define TEST_USERNAME "nobody" -uid_t uid; -gid_t gid; - -int rc; - -void try_bind (void ) +void run (void ) {struct sockaddr_in servaddr; -int sockfd, r_value; - -// Set effective user/group -if ((rc =setegid (gid)) ==-1 ) { -tst_brkm (TBROK |TERRNO ,0 ,"setegid(%u) failed" , gid); - } -if ((rc =seteuid (uid)) ==-1 ) { -tst_brkm (TBROK |TERRNO ,0 ,"seteuid(%u) failed" , uid); - } - -if ((sockfd =socket (AF_INET ,SOCK_STREAM ,0 )) <0 ) { -tst_brkm (TBROK |TERRNO ,0 ,"socket() failed" ); - } +int sockfd; + sockfd =SAFE_SOCKET (AF_INET ,SOCK_STREAM ,0 );memset (&servaddr,0 ,sizeof (servaddr)); servaddr.sin_family =AF_INET ; - servaddr.sin_port =htons (TCP_PRIVLEGED_COM_PORT ); + servaddr.sin_port =htons (TCP_PRIVILEGED_PORT ); servaddr.sin_addr.s_addr =htonl (INADDR_ANY ); - r_value =bind (sockfd, (struct sockaddr *)&servaddr,sizeof (servaddr)); -if (r_value) { -if (errno ==EACCES ) { -tst_resm (TPASS ,"correct error" ); - }else { -tst_resm (TFAIL ,"incorrect error, %d" , r_value); - } - }else { -tst_resm (TFAIL ,"user was able to bind successfully" ); - } - +TEST (bind (sockfd, (struct sockaddr *)&servaddr,sizeof (servaddr)));close (sockfd); -// Set effective user/group -if ((rc =setegid (0 )) ==-1 ) { -tst_brkm (TBROK |TERRNO ,0 ,"setegid(0) reset failed" ); - } -if ((rc =seteuid (uid)) ==-1 ) { -/* XXX: is this seteuid() correct !? it isnt a reset if we -* made the same exact call above ... -*/ -tst_brkm (TBROK |TERRNO ,0 ,"seteuid(%u) reset failed" , uid); +if (!TST_RET ) { +tst_res (TFAIL ,"bind() succeeded unexpectedly." ); + }else if (TST_ERR ==EACCES ) { +tst_res (TPASS ,"bind() failed as expected" ); + }else { +tst_res (TFAIL |TERRNO ,"Unexpected error" ); } - } -int main (int argc,char *argv[]) +void setup (void ) { -char *username ="nobody" ; - -tst_parse_opts (argc, argv,NULL ,NULL ); +struct passwd *pw; +struct group *gr; -tst_require_root (); + pw =SAFE_GETPWNAM (TEST_USERNAME ); + gr =SAFE_GETGRGID (pw->pw_gid); -if ((pw =getpwnam (username)) ==NULL ) { -tst_brkm (TBROK ,0 ,"Username - %s - not found" , username); - } - -if ((gr =getgrgid (pw->pw_gid)) ==NULL ) { -tst_brkm (TBROK |TERRNO ,0 ,"getgrgid(%u) failed" , pw->pw_gid); - } - - uid = pw->pw_uid; - gid = gr->gr_gid; - -tst_resm (TINFO ,"Socket will try to be bind by user: %s, group: %s" , - pw->pw_name, gr->gr_name); +tst_res (TINFO ,"Switching credentials to user: %s, group: %s" , + pw->pw_name, gr->gr_name); +SAFE_SETEGID (gr->gr_gid); +SAFE_SETEUID (pw->pw_uid); +} -try_bind (); -tst_exit (); +void cleanup (void ) { +SAFE_SETEGID (0 ); +SAFE_SETEUID (0 ); } + +static struct tst_test test = { + .test_all = run, + .needs_root =1 , + .setup = setup, + .cleanup = cleanup +};
Signature · 1 line
2.22.1
PATCH 1/2 Update syscalls/bind02 to new API
Hi Martin,
Quoted 1 line
Signed-off-by Martin Doucha <[email protected]>
Apart from unimportant nit style formatting which can be changed by maintainer before import this is nice.
Although it'd be perfectly safe to merge it I guess we wait after LTP release.
Quoted 1 line
testcases/kernel/syscalls/bind/bind02.c | 153 +++++++-----------------
...
testcases/kernel/syscalls/bind/bind02.c line 43
Quoted 2 lines
testcases/kernel/syscalls/bind/bind02.c +2
-0+if (!TST_RET ) { +tst_res (TFAIL ,"bind() succeeded unexpectedly." );
Please, don't use dot.
Quoted 33 lines
testcases/kernel/syscalls/bind/bind02.c +9
-18+ }else if (TST_ERR ==EACCES ) { +tst_res (TPASS ,"bind() failed as expected" ); + }else { +tst_res (TFAIL |TERRNO ,"Unexpected error" ); } - } -int main (int argc,char *argv[]) +void setup (void ) { -char *username ="nobody" ; - -tst_parse_opts (argc, argv,NULL ,NULL ); +struct passwd *pw; +struct group *gr; -tst_require_root (); + pw =SAFE_GETPWNAM (TEST_USERNAME ); + gr =SAFE_GETGRGID (pw->pw_gid); -if ((pw =getpwnam (username)) ==NULL ) { -tst_brkm (TBROK ,0 ,"Username - %s - not found" , username); - } - -if ((gr =getgrgid (pw->pw_gid)) ==NULL ) { -tst_brkm (TBROK |TERRNO ,0 ,"getgrgid(%u) failed" , pw->pw_gid); - } - - uid = pw->pw_uid; - gid = gr->gr_gid; - - tst_resm(TINFO, "Socket will try to be bind by user: %s, group: %s",
testcases/kernel/syscalls/bind/bind02.c line 65
Quoted 10 lines
testcases/kernel/syscalls/bind/bind02.c +6
-3- pw->pw_name, gr->gr_name); +tst_res (TINFO ,"Switching credentials to user: %s, group: %s" , + pw->pw_name, gr->gr_name); +SAFE_SETEGID (gr->gr_gid); +SAFE_SETEUID (pw->pw_uid); +} -try_bind (); -tst_exit (); +void cleanup (void ) {
Bracket should be on separate line.
testcases/kernel/syscalls/bind/bind02.c line 68
Quoted 3 lines
testcases/kernel/syscalls/bind/bind02.c +2
-0+SAFE_SETEGID (0 ); +SAFE_SETEUID (0 ); }
Kind regards,
Petr
PATCH 2/2 Add connection tests for bind()
In reply to Martin DouchaAdd two new test programs to verify that bind() will open sockets for incoming connections. Both programs follow the same test scenario:
- Create and bind() a socket
- Wait for connection from peer thread
- Send request to peer thread
- Receive and verify response from peer thread
bind04 tests stream-oriented sockets (SOCK_STREAM and SOCK_SEQPACKET). bind05 tests datagram-oriented sockets (SOCK_DGRAM).
Both programs test the following socket types:
- AF_UNIX (pathname and abstract addresses)
- AF_INET (loopback)
- AF_INET6 (loopback)
--- runtest/syscalls | 2 + testcases/kernel/syscalls/bind/.gitignore | 2 + testcases/kernel/syscalls/bind/Makefile | 5 + testcases/kernel/syscalls/bind/bind04.c | 206 ++++++++++++++++++++ testcases/kernel/syscalls/bind/bind05.c | 219 ++++++++++++++++++++++ testcases/kernel/syscalls/bind/libbind.c | 47 +++++ testcases/kernel/syscalls/bind/libbind.h | 15 ++ 7 files changed, 496 insertions(+) create mode 100644 testcases/kernel/syscalls/bind/bind04.c create mode 100644 testcases/kernel/syscalls/bind/bind05.c create mode 100644 testcases/kernel/syscalls/bind/libbind.c create mode 100644 testcases/kernel/syscalls/bind/libbind.h
runtest/syscalls +2 -0
@@ -31,6 +31,8 @@ alarm07 alarm07 bind01 bind01 bind02 bind02 bind03 bind03 +bind04 bind04 +bind05 bind05 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01
testcases/kernel/syscalls/bind/.gitignore +2 -0
@@ -1,3 +1,5 @@ /bind01 /bind02 /bind03 +/bind04 +/bind05
testcases/kernel/syscalls/bind/Makefile +5 -0
@@ -20,4 +20,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +FILTER_OUT_MAKE_TARGETS := libbind +bind04 bind05: LDLIBS += -lpthread + include $(top_srcdir)/include/mk/generic_leaf_target.mk + +$(MAKE_TARGETS): %: %.o libbind.o
testcases/kernel/syscalls/bind/bind04.c new file +206 -0
206 lines not on this page · raw message
testcases/kernel/syscalls/bind/bind05.c new file +219 -0
219 lines not on this page · raw message
testcases/kernel/syscalls/bind/libbind.c new file +47 -0
47 lines not on this page · raw message
testcases/kernel/syscalls/bind/libbind.h new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Martin Doucha <[email protected]> +*/ + +#include <sys/types.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <netinet/ip.h> + +/* +* Find valid address for connect()ing to a given bound socket +* Wildcard addresses like INADDR_ANY will be replaced with localhost +*/ +socklen_t get_connect_address (int sock,struct sockaddr_storage *addr);
Signature · 1 line
2.22.1
PATCH 2/2 Add connection tests for bind()
Hi Martin,
Quoted 7 lines
Add two new test programs to verify that
bind()will open sockets for incoming connections. Both programs follow the same test scenario:
- Create andbind()a socket
- Wait for connection from peer thread
- Send request to peer thread
- Receive and verify response from peer thread
Quoted 7 lines
bind04 tests stream-oriented sockets (
SOCK_STREAMandSOCK_SEQPACKET). bind05 tests datagram-oriented sockets (SOCK_DGRAM).Both programs test the following socket types:
-AF_UNIX(pathname and abstract addresses)
-AF_INET(loopback)
-AF_INET6(loopback)
I'd drop libbind.c and move content into libbind.h, using inline functions.
Both tests also share a lot of code. I understand you don't want to mix TCP and UDP tests (I would probably do), but could you at least move setup() and constants into libbind.h?
I'd also test:IPPROTO_SCTP (SOCK_STREAM) for TCP andIPPROTO_UDPLITE (SOCK_DGRAM) for UDP.
+ test some protocols for other sock types:IPPROTO_SCTP (SOCK_STREAM).
Quoted 1 line
Signed-off-by Martin Doucha <[email protected]>
Kind regards,
Petr