1 /* 2 * Copyright (c) 2017 Google, Inc. 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, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program, if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 /* 19 * Regression test for: 20 * 21 * commit e645016abc80 ("KEYS: fix writing past end of user-supplied buffer 22 * in keyring_read()"). 23 * 24 * as well as its follow-on fix: 25 * 26 * commit 3239b6f29bdf ("KEYS: return full count in keyring_read() if 27 * buffer is too small") 28 * 29 */ 30 31 #include <errno.h> 32 33 #include "tst_test.h" 34 #include "lapi/keyctl.h" 35 36 static void add_test_key(const char *description) 37 { 38 TEST(add_key("user", description, "payload", 7, 39 KEY_SPEC_PROCESS_KEYRING)); 40 if (TEST_RETURN < 0) 41 tst_brk(TBROK | TTERRNO, "Failed to add test key"); 42 } 43 44 static void do_test(void) 45 { 46 key_serial_t key_ids[2]; 47 48 add_test_key("key1"); 49 add_test_key("key2"); 50 51 memset(key_ids, 0, sizeof(key_ids)); 52 TEST(keyctl(KEYCTL_READ, KEY_SPEC_PROCESS_KEYRING, 53 (char *)key_ids, sizeof(key_serial_t))); 54 if (TEST_RETURN < 0) 55 tst_brk(TBROK | TTERRNO, "KEYCTL_READ failed"); 56 57 /* 58 * Do not check key_ids[0], as the contents of the buffer are 59 * unspecified if it was too small. However, key_ids[1] must not have 60 * been written to, as it was outside the buffer. 61 */ 62 63 if (key_ids[1] != 0) 64 tst_brk(TFAIL, "KEYCTL_READ overran the buffer"); 65 66 if (TEST_RETURN != sizeof(key_ids)) { 67 tst_brk(TFAIL, "KEYCTL_READ returned %ld but expected %zu", 68 TEST_RETURN, sizeof(key_ids)); 69 } 70 71 tst_res(TPASS, 72 "KEYCTL_READ returned full count but didn't overrun the buffer"); 73 } 74 75 static struct tst_test test = { 76 .test_all = do_test, 77 }; 78