subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 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> |
yaberauneya | 4dfb280 | 2009-12-06 11:54:42 +0000 | [diff] [blame] | 24 | #include <netinet/ip.h> |
| 25 | #include <netinet/udp.h> |
| 26 | #ifdef HAVE_LINUX_NETLINK_H |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 27 | #include <linux/netlink.h> |
yaberauneya | 4dfb280 | 2009-12-06 11:54:42 +0000 | [diff] [blame] | 28 | #endif |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 29 | |
| 30 | #include <sys/xattr.h> |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 31 | #include <sys/vfs.h> |
| 32 | |
| 33 | #include "test.h" |
| 34 | #include "usctest.h" |
| 35 | |
| 36 | char *TCID = "smack_set_socket_labels"; |
| 37 | int TST_TOTAL = 1; |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 38 | |
Zeng Linggang | ea39277 | 2015-01-21 11:11:58 +0800 | [diff] [blame] | 39 | static void setup(void); |
| 40 | static void cleanup(void); |
| 41 | static void set_socket_labels(char **); |
| 42 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 43 | int main(int argc, char *argv[]) |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 44 | { |
Zeng Linggang | ea39277 | 2015-01-21 11:11:58 +0800 | [diff] [blame] | 45 | int lc; |
| 46 | const char *msg; |
| 47 | |
| 48 | msg = parse_opts(argc, argv, NULL, NULL); |
| 49 | if (msg != NULL) |
| 50 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 51 | |
| 52 | setup(); |
| 53 | |
| 54 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 55 | tst_count = 0; |
| 56 | set_socket_labels(argv); |
| 57 | } |
| 58 | |
| 59 | cleanup(); |
| 60 | tst_exit(); |
| 61 | } |
| 62 | |
| 63 | static void setup(void) |
| 64 | { |
| 65 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 66 | |
| 67 | TEST_PAUSE; |
| 68 | } |
| 69 | |
| 70 | static void set_socket_labels(char **argv) |
| 71 | { |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 72 | char *anin = "security.SMACK64IPIN"; |
| 73 | char *anout = "security.SMACK64IPOUT"; |
| 74 | char *annot = "security.SMACK64IPNOT"; |
| 75 | char *avin = "TheOne"; |
| 76 | char *avout = "TheOther"; |
| 77 | char *avnot = "TheBadValue"; |
| 78 | int sock; |
| 79 | int rc; |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 80 | char buf[256]; |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 81 | |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 82 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 83 | if (sock < 0) |
| 84 | tst_brkm(TFAIL, NULL, "%s Socket creation failure", argv[0]); |
| 85 | |
| 86 | flistxattr(sock, buf, 256); |
| 87 | if (strstr(buf, "security.SMACK64") == NULL) |
| 88 | tst_brkm(TCONF, NULL, "smackfs not set."); |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 89 | |
| 90 | rc = fsetxattr(sock, anin, avin, strlen(avin) + 1, 0); |
| 91 | if (rc < 0) { |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 92 | tst_brkm(TFAIL, NULL, "%s fsetxattr of %s to %s failure", |
| 93 | argv[0], anin, avin); |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | rc = fsetxattr(sock, anout, avout, strlen(avout) + 1, 0); |
| 97 | if (rc < 0) { |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 98 | tst_brkm(TFAIL, NULL, "%s fsetxattr of %s to %s failure", |
| 99 | argv[0], anout, avout); |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | rc = fsetxattr(sock, annot, avnot, strlen(avnot) + 1, 0); |
| 103 | if (rc >= 0) { |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 104 | tst_brkm(TFAIL, NULL, |
| 105 | "%s fsetxattr of %s to %s succeeded in error", |
| 106 | argv[0], anout, avout); |
subrata_modak | 6c7dfab | 2009-03-19 07:27:04 +0000 | [diff] [blame] | 107 | } |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 108 | |
| 109 | tst_resm(TPASS, "Test %s success.", TCID); |
Zeng Linggang | ea39277 | 2015-01-21 11:11:58 +0800 | [diff] [blame] | 110 | } |
Zeng Linggang | 5dc669f | 2015-01-20 16:32:41 +0800 | [diff] [blame] | 111 | |
Zeng Linggang | ea39277 | 2015-01-21 11:11:58 +0800 | [diff] [blame] | 112 | static void cleanup(void) |
| 113 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 114 | } |