1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include "test_netlink.h"
15 #include <linux/selinux_netlink.h>
16
17 static void
18 test_nlmsg_type(const int fd)
19 {
20 long rc;
21 struct nlmsghdr nlh = {
22 .nlmsg_len = sizeof(nlh),
23 .nlmsg_type = SELNL_MSG_SETENFORCE,
24 .nlmsg_flags = NLM_F_REQUEST,
25 };
26
27 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
28 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=SELNL_MSG_SETENFORCE"
29 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
30 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
31 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
32 }
33
34 static void
35 test_selnl_msg_unspec(const int fd)
36 {
37 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, 4);
38
39 TEST_NETLINK_(fd, nlh0,
40 0xffff, "0xffff /* SELNL_MSG_??? */",
41 NLM_F_REQUEST, "NLM_F_REQUEST",
42 4, "1234", 4,
43 printf("\"\\x31\\x32\\x33\\x34\""));
44 }
45
46 static void
47 test_selnl_msg_setenforce(const int fd)
48 {
49 static const struct selnl_msg_setenforce msg = {
50 .val = 0xfbdcdfab
51 };
52 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
53
54 TEST_NETLINK_OBJECT(fd, nlh0,
55 SELNL_MSG_SETENFORCE, NLM_F_REQUEST, msg,
56 printf("{");
57 PRINT_FIELD_D(msg, val);
58 printf("}"));
59 }
60
61 static void
62 test_selnl_msg_policyload(const int fd)
63 {
64 static const struct selnl_msg_policyload msg = {
65 .seqno = 0xabdcfabc
66 };
67 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));
68
69 TEST_NETLINK_OBJECT(fd, nlh0,
70 SELNL_MSG_POLICYLOAD, NLM_F_REQUEST, msg,
71 printf("{");
72 PRINT_FIELD_U(msg, seqno);
73 printf("}"));
74 }
75
76 int main(void)
77 {
78 skip_if_unavailable("/proc/self/fd/");
79
80 int fd = create_nl_socket(NETLINK_SELINUX);
81
82 test_nlmsg_type(fd);
83 test_selnl_msg_unspec(fd);
84 test_selnl_msg_setenforce(fd);
85 test_selnl_msg_policyload(fd);
86
87 printf("+++ exited with 0 +++\n");
88
89 return 0;
90 }