1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2004
4*49cdfc7eSAndroid Build Coastguard Worker * 07/2004 Written by Dan Jones
5*49cdfc7eSAndroid Build Coastguard Worker * 07/2004 Ported to LTP format by Robbie Williamson
6*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 Martin Doucha <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker /*
10*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
11*49cdfc7eSAndroid Build Coastguard Worker * Make sure bind() of privileged port gives EACCESS error for non-root users.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker /* This port needs to be a privileged port */
26*49cdfc7eSAndroid Build Coastguard Worker #define TCP_PRIVILEGED_PORT 463
27*49cdfc7eSAndroid Build Coastguard Worker #define TEST_USERNAME "nobody"
28*49cdfc7eSAndroid Build Coastguard Worker
run(void)29*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in servaddr;
32*49cdfc7eSAndroid Build Coastguard Worker int sockfd;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker sockfd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
35*49cdfc7eSAndroid Build Coastguard Worker memset(&servaddr, 0, sizeof(servaddr));
36*49cdfc7eSAndroid Build Coastguard Worker servaddr.sin_family = AF_INET;
37*49cdfc7eSAndroid Build Coastguard Worker servaddr.sin_port = htons(TCP_PRIVILEGED_PORT);
38*49cdfc7eSAndroid Build Coastguard Worker servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
39*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)),
40*49cdfc7eSAndroid Build Coastguard Worker EACCES, "bind()");
41*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(sockfd);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
setup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker struct passwd *pw;
47*49cdfc7eSAndroid Build Coastguard Worker struct group *gr;
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker pw = SAFE_GETPWNAM(TEST_USERNAME);
50*49cdfc7eSAndroid Build Coastguard Worker gr = SAFE_GETGRGID(pw->pw_gid);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Switching credentials to user: %s, group: %s",
53*49cdfc7eSAndroid Build Coastguard Worker pw->pw_name, gr->gr_name);
54*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEGID(gr->gr_gid);
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(pw->pw_uid);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
60*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
61*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
62*49cdfc7eSAndroid Build Coastguard Worker };
63