2 * Check decoding of add_key syscall.
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <asm/unistd.h>
36 # include <inttypes.h>
41 print_val_str(const void *ptr, const char *str)
50 do_add_key(const char *type, const char *type_str, const char *desc,
51 const char *desc_str, const char *payload, const char *payload_str,
52 size_t plen, int32_t keyring, const char *keyring_str)
54 long rc = syscall(__NR_add_key, type, desc, payload, plen, keyring);
55 const char *errstr = sprintrc(rc);
57 print_val_str(type, type_str);
58 print_val_str(desc, desc_str);
59 print_val_str(payload, payload_str);
60 printf("%zu, ", plen);
62 printf("%s", keyring_str);
64 printf("%d", keyring);
65 printf(") = %s\n", errstr);
71 static const char unterminated1[] = { '\1', '\2', '\3', '\4', '\5' };
72 static const char unterminated2[] = { '\6', '\7', '\10', '\11', '\12' };
73 static const char unterminated3[] = { '\16', '\17', '\20', '\21', '\22' };
75 char *bogus_type = tail_memdup(unterminated1, sizeof(unterminated1));
76 char *bogus_desc = tail_memdup(unterminated2, sizeof(unterminated2));
77 char *bogus_payload = tail_memdup(unterminated3, sizeof(unterminated3));
89 { bogus_type + sizeof(unterminated1), NULL },
91 { ARG_STR("\20\21\22\23\24") },
100 { bogus_desc + sizeof(unterminated2), NULL },
101 { bogus_desc, NULL },
102 { ARG_STR("\25\26\27\30\31") },
104 { "overly long description", _STR("overly long ") "..." },
112 { ARG_STR(NULL), 0 },
113 { bogus_payload + sizeof(unterminated3), NULL,
114 (size_t) 0xdeadbeefbadc0dedULL },
115 { bogus_payload, _STR(""), 0 },
116 { bogus_payload, _STR("\16\17\20\21\22"), 5 },
117 { bogus_payload, NULL, 10 },
118 { "overly long payload", _STR("overly long ") "...", 15 },
126 { ARG_STR(1234567890) },
127 { ARG_STR(-1234567890) },
128 { -1, "KEY_SPEC_THREAD_KEYRING" },
131 for (i = 0; i < ARRAY_SIZE(types); i++)
132 for (j = 0; j < ARRAY_SIZE(descs); j++)
133 for (k = 0; k < ARRAY_SIZE(payloads); k++)
134 for (l = 0; l < ARRAY_SIZE(keyrings); l++)
135 do_add_key(types[i].type, types[i].str,
136 descs[j].desc, descs[j].str,
143 puts("+++ exited with 0 +++");
150 SKIP_MAIN_UNDEFINED("__NR_add_key");