1 /****************************************************************************** 2 * Copyright (c) Crackerjack Project., 2007 * 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 * 12 * the 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, write to the Free Software Foundation, * 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 17 * * 18 ******************************************************************************/ 19 /* 20 * Description: This tests the keyctl() syscall 21 * Manipulate the kernel's key management facility 22 * 23 * History: Porting from Crackerjack to LTP is done by 24 * Manas Kumar Nayak maknayak (at) in.ibm.com> 25 */ 26 27 #include "config.h" 28 #include <sys/types.h> 29 #include <errno.h> 30 #include <limits.h> 31 #include <stdio.h> 32 #include <stdint.h> 33 #ifdef HAVE_LINUX_KEYCTL_H 34 # include <linux/keyctl.h> 35 #endif 36 37 #include "test.h" 38 #include "linux_syscall_numbers.h" 39 40 char *TCID = "keyctl01"; 41 int testno; 42 int TST_TOTAL = 2; 43 44 #ifdef HAVE_LINUX_KEYCTL_H 45 46 static void cleanup(void) 47 { 48 tst_rmdir(); 49 } 50 51 static void setup(void) 52 { 53 TEST_PAUSE; 54 tst_tmpdir(); 55 } 56 57 int main(int ac, char **av) 58 { 59 int ret; 60 int lc; 61 int32_t ne_key; 62 63 tst_parse_opts(ac, av, NULL, NULL); 64 65 setup(); 66 67 for (lc = 0; TEST_LOOPING(lc); lc++) { 68 69 tst_count = 0; 70 71 for (testno = 1; testno < TST_TOTAL; ++testno) { 72 73 /* Call keyctl() and ask for a keyring's ID. */ 74 ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, 75 KEY_SPEC_USER_SESSION_KEYRING); 76 if (ret != -1) { 77 tst_resm(TPASS, 78 "KEYCTL_GET_KEYRING_ID succeeded"); 79 } else { 80 tst_resm(TFAIL | TERRNO, 81 "KEYCTL_GET_KEYRING_ID"); 82 } 83 84 for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) { 85 ret = ltp_syscall(__NR_keyctl, KEYCTL_READ, 86 ne_key); 87 if (ret == -1 && errno == ENOKEY) 88 break; 89 } 90 91 /* Call keyctl. */ 92 ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key); 93 if (ret != -1) { 94 tst_resm(TFAIL | TERRNO, 95 "KEYCTL_REVOKE succeeded unexpectedly"); 96 } else { 97 /* Check for the correct error num. */ 98 if (errno == ENOKEY) { 99 tst_resm(TPASS | TERRNO, 100 "KEYCTL_REVOKE got expected " 101 "errno"); 102 } else { 103 tst_resm(TFAIL | TERRNO, 104 "KEYCTL_REVOKE got unexpected " 105 "errno"); 106 } 107 108 } 109 110 } 111 112 } 113 cleanup(); 114 tst_exit(); 115 } 116 #else 117 int main(void) 118 { 119 tst_brkm(TCONF, NULL, "keyctl syscall support not available on system"); 120 } 121 #endif /* HAVE_LINUX_KEYCTL_H */ 122