1 /*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
7 *
8 * Author:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netinet/ip.h>
25 #include <netinet/udp.h>
26 #ifdef HAVE_LINUX_NETLINK_H
27 #include <linux/netlink.h>
28 #endif
29
30 #include <sys/xattr.h>
31 #include <sys/vfs.h>
32
33 #include "test.h"
34
35 char *TCID = "smack_set_socket_labels";
36 int TST_TOTAL = 1;
37
38 static void setup(void);
39 static void cleanup(void);
40 static void set_socket_labels(char **);
41
main(int argc,char * argv[])42 int main(int argc, char *argv[])
43 {
44 int lc;
45
46 tst_parse_opts(argc, argv, NULL, NULL);
47
48 setup();
49
50 for (lc = 0; TEST_LOOPING(lc); lc++) {
51 tst_count = 0;
52 set_socket_labels(argv);
53 }
54
55 cleanup();
56 tst_exit();
57 }
58
setup(void)59 static void setup(void)
60 {
61 tst_sig(NOFORK, DEF_HANDLER, cleanup);
62
63 TEST_PAUSE;
64 }
65
set_socket_labels(char ** argv)66 static void set_socket_labels(char **argv)
67 {
68 char *anin = "security.SMACK64IPIN";
69 char *anout = "security.SMACK64IPOUT";
70 char *annot = "security.SMACK64IPNOT";
71 char *avin = "TheOne";
72 char *avout = "TheOther";
73 char *avnot = "TheBadValue";
74 int sock;
75 int rc;
76 char buf[256];
77
78 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
79 if (sock < 0)
80 tst_brkm(TFAIL, NULL, "%s Socket creation failure", argv[0]);
81
82 flistxattr(sock, buf, 256);
83 if (strstr(buf, "security.SMACK64") == NULL)
84 tst_brkm(TCONF, NULL, "smackfs not set.");
85
86 rc = fsetxattr(sock, anin, avin, strlen(avin) + 1, 0);
87 if (rc < 0) {
88 tst_brkm(TFAIL, NULL, "%s fsetxattr of %s to %s failure",
89 argv[0], anin, avin);
90 }
91
92 rc = fsetxattr(sock, anout, avout, strlen(avout) + 1, 0);
93 if (rc < 0) {
94 tst_brkm(TFAIL, NULL, "%s fsetxattr of %s to %s failure",
95 argv[0], anout, avout);
96 }
97
98 rc = fsetxattr(sock, annot, avnot, strlen(avnot) + 1, 0);
99 if (rc >= 0) {
100 tst_brkm(TFAIL, NULL,
101 "%s fsetxattr of %s to %s succeeded in error",
102 argv[0], anout, avout);
103 }
104
105 tst_resm(TPASS, "Test %s success.", TCID);
106 }
107
cleanup(void)108 static void cleanup(void)
109 {
110 }
111