1 /* 2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com> 3 * Copyright (c) 2017-2018 The strace developers. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "tests.h" 30 #include <stdio.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <sys/socket.h> 34 #include "test_netlink.h" 35 #include <linux/selinux_netlink.h> 36 37 static void 38 test_nlmsg_type(const int fd) 39 { 40 long rc; 41 struct nlmsghdr nlh = { 42 .nlmsg_len = sizeof(nlh), 43 .nlmsg_type = SELNL_MSG_SETENFORCE, 44 .nlmsg_flags = NLM_F_REQUEST, 45 }; 46 47 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0); 48 printf("sendto(%d, {len=%u, type=SELNL_MSG_SETENFORCE" 49 ", flags=NLM_F_REQUEST, seq=0, pid=0}" 50 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", 51 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc)); 52 } 53 54 static void 55 test_selnl_msg_unspec(const int fd) 56 { 57 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, 4); 58 59 TEST_NETLINK_(fd, nlh0, 60 0xffff, "0xffff /* SELNL_MSG_??? */", 61 NLM_F_REQUEST, "NLM_F_REQUEST", 62 4, "1234", 4, 63 printf("\"\\x31\\x32\\x33\\x34\"")); 64 } 65 66 static void 67 test_selnl_msg_setenforce(const int fd) 68 { 69 static const struct selnl_msg_setenforce msg = { 70 .val = 0xfbdcdfab 71 }; 72 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); 73 74 TEST_NETLINK_OBJECT(fd, nlh0, 75 SELNL_MSG_SETENFORCE, NLM_F_REQUEST, msg, 76 PRINT_FIELD_D("{", msg, val); 77 printf("}")); 78 } 79 80 static void 81 test_selnl_msg_policyload(const int fd) 82 { 83 static const struct selnl_msg_policyload msg = { 84 .seqno = 0xabdcfabc 85 }; 86 void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg)); 87 88 TEST_NETLINK_OBJECT(fd, nlh0, 89 SELNL_MSG_POLICYLOAD, NLM_F_REQUEST, msg, 90 PRINT_FIELD_U("{", msg, seqno); 91 printf("}")); 92 } 93 94 int main(void) 95 { 96 skip_if_unavailable("/proc/self/fd/"); 97 98 int fd = create_nl_socket(NETLINK_SELINUX); 99 100 test_nlmsg_type(fd); 101 test_selnl_msg_unspec(fd); 102 test_selnl_msg_setenforce(fd); 103 test_selnl_msg_policyload(fd); 104 105 printf("+++ exited with 0 +++\n"); 106 107 return 0; 108 } 109