diff options
author | Armin Kuster <akuster808@gmail.com> | 2019-05-06 11:36:58 -0700 |
---|---|---|
committer | Armin Kuster <akuster808@gmail.com> | 2019-05-09 17:45:13 -0700 |
commit | 8eee8727cb09a9fc14e899b4058fcd108f44a0eb (patch) | |
tree | e6cd90e7b08c098e425a8d0efbb29fd88fc9ee2e /recipes-mac/smack/udp-smack-test | |
parent | 5d37937f2e495147fd2a756d22c09f49773ac8ae (diff) | |
download | meta-security-8eee8727cb09a9fc14e899b4058fcd108f44a0eb.tar.gz |
smack-test: add smack tests from meta-intel-iot-security
ported over smack tests
Signed-off-by: Armin Kuster <akuster808@gmail.com>
Diffstat (limited to 'recipes-mac/smack/udp-smack-test')
-rw-r--r-- | recipes-mac/smack/udp-smack-test/test_smack_udp_sockets.sh | 107 | ||||
-rw-r--r-- | recipes-mac/smack/udp-smack-test/udp_client.c | 75 | ||||
-rw-r--r-- | recipes-mac/smack/udp-smack-test/udp_server.c | 93 |
3 files changed, 275 insertions, 0 deletions
diff --git a/recipes-mac/smack/udp-smack-test/test_smack_udp_sockets.sh b/recipes-mac/smack/udp-smack-test/test_smack_udp_sockets.sh new file mode 100644 index 0000000..419ab9f --- /dev/null +++ b/recipes-mac/smack/udp-smack-test/test_smack_udp_sockets.sh | |||
@@ -0,0 +1,107 @@ | |||
1 | #!/bin/sh | ||
2 | RC=0 | ||
3 | test_file="/tmp/smack_socket_udp" | ||
4 | SMACK_PATH=`grep smack /proc/mounts | awk '{print $2}' ` | ||
5 | |||
6 | udp_server=`which udp_server` | ||
7 | if [ -z $udp_server ]; then | ||
8 | if [ -f "/tmp/udp_server" ]; then | ||
9 | udp_server="/tmp/udp_server" | ||
10 | else | ||
11 | echo "udp_server binary not found" | ||
12 | exit 1 | ||
13 | fi | ||
14 | fi | ||
15 | udp_client=`which udp_client` | ||
16 | if [ -z $udp_client ]; then | ||
17 | if [ -f "/tmp/udp_client" ]; then | ||
18 | udp_client="/tmp/udp_client" | ||
19 | else | ||
20 | echo "udp_client binary not found" | ||
21 | exit 1 | ||
22 | fi | ||
23 | fi | ||
24 | |||
25 | # make sure no access is granted | ||
26 | # 12345678901234567890123456789012345678901234567890123456 | ||
27 | echo -n "label1 label2 -----" > $SMACK_PATH/load | ||
28 | |||
29 | # checking access for sockets with different labels | ||
30 | $udp_server 50021 label2 2>$test_file & | ||
31 | server_pid=$! | ||
32 | sleep 1 | ||
33 | $udp_client 50021 label1 2>$test_file & | ||
34 | client_pid=$! | ||
35 | wait $server_pid | ||
36 | server_rv=$? | ||
37 | wait $client_pid | ||
38 | client_rv=$? | ||
39 | if [ $server_rv -eq 0 ]; then | ||
40 | echo "Sockets with different labels should not communicate on udp" | ||
41 | exit 1 | ||
42 | fi | ||
43 | |||
44 | # granting access between different labels | ||
45 | # 12345678901234567890123456789012345678901234567890123456 | ||
46 | echo -n "label1 label2 rw---" > $SMACK_PATH/load | ||
47 | # checking access for sockets with different labels, but having a rule granting rw | ||
48 | $udp_server 50022 label2 2>$test_file & | ||
49 | server_pid=$! | ||
50 | sleep 1 | ||
51 | $udp_client 50022 label1 2>$test_file & | ||
52 | client_pid=$! | ||
53 | wait $server_pid | ||
54 | server_rv=$? | ||
55 | wait $client_pid | ||
56 | client_rv=$? | ||
57 | if [ $server_rv -ne 0 -o $client_rv -ne 0 ]; then | ||
58 | echo "Sockets with different labels, but having rw access, should communicate on udp" | ||
59 | exit 1 | ||
60 | fi | ||
61 | |||
62 | # checking access for sockets with the same label | ||
63 | $udp_server 50023 label1 & | ||
64 | server_pid=$! | ||
65 | sleep 1 | ||
66 | $udp_client 50023 label1 2>$test_file & | ||
67 | client_pid=$! | ||
68 | wait $server_pid | ||
69 | server_rv=$? | ||
70 | wait $client_pid | ||
71 | client_rv=$? | ||
72 | if [ $server_rv -ne 0 -o $client_rv -ne 0 ]; then | ||
73 | echo "Sockets with same labels should communicate on udp" | ||
74 | exit 1 | ||
75 | fi | ||
76 | |||
77 | # checking access on socket labeled star (*) | ||
78 | # should always be permitted | ||
79 | $udp_server 50024 \* 2>$test_file & | ||
80 | server_pid=$! | ||
81 | sleep 1 | ||
82 | $udp_client 50024 label1 2>$test_file & | ||
83 | client_pid=$! | ||
84 | wait $server_pid | ||
85 | server_rv=$? | ||
86 | wait $client_pid | ||
87 | client_rv=$? | ||
88 | if [ $server_rv -ne 0 -o $client_rv -ne 0 ]; then | ||
89 | echo "Should have access on udp socket labeled star (*)" | ||
90 | exit 1 | ||
91 | fi | ||
92 | |||
93 | # checking access from socket labeled star (*) | ||
94 | # all access from subject star should be denied | ||
95 | $udp_server 50025 label1 2>$test_file & | ||
96 | server_pid=$! | ||
97 | sleep 1 | ||
98 | $udp_client 50025 \* 2>$test_file & | ||
99 | client_pid=$! | ||
100 | wait $server_pid | ||
101 | server_rv=$? | ||
102 | wait $client_pid | ||
103 | client_rv=$? | ||
104 | if [ $server_rv -eq 0 ]; then | ||
105 | echo "Socket labeled star should not have access to any udp socket" | ||
106 | exit 1 | ||
107 | fi | ||
diff --git a/recipes-mac/smack/udp-smack-test/udp_client.c b/recipes-mac/smack/udp-smack-test/udp_client.c new file mode 100644 index 0000000..4d3afbe --- /dev/null +++ b/recipes-mac/smack/udp-smack-test/udp_client.c | |||
@@ -0,0 +1,75 @@ | |||
1 | // (C) Copyright 2015 Intel Corporation | ||
2 | // | ||
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4 | // of this software and associated documentation files (the "Software"), to deal | ||
5 | // in the Software without restriction, including without limitation the rights | ||
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
7 | // copies of the Software, and to permit persons to whom the Software is | ||
8 | // furnished to do so, subject to the following conditions: | ||
9 | // | ||
10 | // The above copyright notice and this permission notice shall be included in | ||
11 | // all copies or substantial portions of the Software. | ||
12 | // | ||
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
19 | // THE SOFTWARE. | ||
20 | #include <sys/socket.h> | ||
21 | #include <stdio.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <netdb.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | int main(int argc, char* argv[]) | ||
27 | { | ||
28 | char* message = "hello"; | ||
29 | int sock, ret; | ||
30 | struct sockaddr_in server_addr; | ||
31 | struct hostent* host = gethostbyname("localhost"); | ||
32 | char* label; | ||
33 | char* attr = "security.SMACK64IPOUT"; | ||
34 | int port; | ||
35 | if (argc != 3) | ||
36 | { | ||
37 | perror("Client: Argument missing, please provide port and label for SMACK64IPOUT"); | ||
38 | return 2; | ||
39 | } | ||
40 | |||
41 | port = atoi(argv[1]); | ||
42 | label = argv[2]; | ||
43 | sock = socket(AF_INET, SOCK_DGRAM,0); | ||
44 | if(sock < 0) | ||
45 | { | ||
46 | perror("Client: Socket failure"); | ||
47 | return 2; | ||
48 | } | ||
49 | |||
50 | |||
51 | if(fsetxattr(sock, attr, label, strlen(label),0) < 0) | ||
52 | { | ||
53 | perror("Client: Unable to set attribute "); | ||
54 | return 2; | ||
55 | } | ||
56 | |||
57 | |||
58 | server_addr.sin_family = AF_INET; | ||
59 | server_addr.sin_port = htons(port); | ||
60 | bcopy((char*) host->h_addr, (char*) &server_addr.sin_addr.s_addr,host->h_length); | ||
61 | bzero(&(server_addr.sin_zero),8); | ||
62 | |||
63 | ret = sendto(sock, message, strlen(message),0,(const struct sockaddr*)&server_addr, | ||
64 | sizeof(struct sockaddr_in)); | ||
65 | |||
66 | close(sock); | ||
67 | if(ret < 0) | ||
68 | { | ||
69 | perror("Client: Error sending message\n"); | ||
70 | return 1; | ||
71 | } | ||
72 | |||
73 | return 0; | ||
74 | } | ||
75 | |||
diff --git a/recipes-mac/smack/udp-smack-test/udp_server.c b/recipes-mac/smack/udp-smack-test/udp_server.c new file mode 100644 index 0000000..cbab71e --- /dev/null +++ b/recipes-mac/smack/udp-smack-test/udp_server.c | |||
@@ -0,0 +1,93 @@ | |||
1 | // (C) Copyright 2015 Intel Corporation | ||
2 | // | ||
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4 | // of this software and associated documentation files (the "Software"), to deal | ||
5 | // in the Software without restriction, including without limitation the rights | ||
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
7 | // copies of the Software, and to permit persons to whom the Software is | ||
8 | // furnished to do so, subject to the following conditions: | ||
9 | // | ||
10 | // The above copyright notice and this permission notice shall be included in | ||
11 | // all copies or substantial portions of the Software. | ||
12 | // | ||
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
19 | // THE SOFTWARE. | ||
20 | #include <sys/socket.h> | ||
21 | #include <stdio.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <netdb.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | int main(int argc, char* argv[]) | ||
27 | { | ||
28 | int sock,ret; | ||
29 | struct sockaddr_in server_addr, client_addr; | ||
30 | socklen_t len; | ||
31 | char message[5]; | ||
32 | char* label; | ||
33 | char* attr = "security.SMACK64IPIN"; | ||
34 | int port; | ||
35 | |||
36 | if(argc != 3) | ||
37 | { | ||
38 | perror("Server: Argument missing, please provide port and label for SMACK64IPIN"); | ||
39 | return 2; | ||
40 | } | ||
41 | |||
42 | port = atoi(argv[1]); | ||
43 | label = argv[2]; | ||
44 | |||
45 | struct timeval timeout; | ||
46 | timeout.tv_sec = 15; | ||
47 | timeout.tv_usec = 0; | ||
48 | |||
49 | sock = socket(AF_INET,SOCK_DGRAM,0); | ||
50 | if(sock < 0) | ||
51 | { | ||
52 | perror("Server: Socket error"); | ||
53 | return 2; | ||
54 | } | ||
55 | |||
56 | |||
57 | if(fsetxattr(sock, attr, label, strlen(label), 0) < 0) | ||
58 | { | ||
59 | perror("Server: Unable to set attribute "); | ||
60 | return 2; | ||
61 | } | ||
62 | |||
63 | server_addr.sin_family = AF_INET; | ||
64 | server_addr.sin_port = htons(port); | ||
65 | server_addr.sin_addr.s_addr = INADDR_ANY; | ||
66 | bzero(&(server_addr.sin_zero),8); | ||
67 | |||
68 | |||
69 | if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) | ||
70 | { | ||
71 | perror("Server: Set timeout failed\n"); | ||
72 | return 2; | ||
73 | } | ||
74 | |||
75 | if(bind(sock, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) | ||
76 | { | ||
77 | perror("Server: Bind failure"); | ||
78 | return 2; | ||
79 | } | ||
80 | |||
81 | len = sizeof(client_addr); | ||
82 | ret = recvfrom(sock, message, sizeof(message), 0, (struct sockaddr*)&client_addr, | ||
83 | &len); | ||
84 | close(sock); | ||
85 | if(ret < 0) | ||
86 | { | ||
87 | perror("Server: Error receiving"); | ||
88 | return 1; | ||
89 | |||
90 | } | ||
91 | return 0; | ||
92 | } | ||
93 | |||