Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 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@in.ibm.com> |
| 25 | */ |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 26 | |
Garrett Cooper | 020a4da | 2010-11-03 20:57:21 -0700 | [diff] [blame] | 27 | #include "config.h" |
Garrett Cooper | 6340c2a | 2010-11-01 00:16:19 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <limits.h> |
| 31 | #include <stdio.h> |
Cyril Hrubis | 988aabf | 2014-08-20 17:33:21 +0200 | [diff] [blame] | 32 | #include <stdint.h> |
| 33 | #ifdef HAVE_LINUX_KEYCTL_H |
| 34 | # include <linux/keyctl.h> |
| 35 | #endif |
Garrett Cooper | 4e77264 | 2010-10-31 20:58:03 -0700 | [diff] [blame] | 36 | |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 37 | #include "test.h" |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 38 | #include "linux_syscall_numbers.h" |
| 39 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 40 | char *TCID = "keyctl01"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 41 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 42 | int TST_TOTAL = 2; |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 43 | |
Cyril Hrubis | 988aabf | 2014-08-20 17:33:21 +0200 | [diff] [blame] | 44 | #ifdef HAVE_LINUX_KEYCTL_H |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 45 | |
Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 46 | static void cleanup(void) |
| 47 | { |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 48 | tst_rmdir(); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 51 | static void setup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | { |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 53 | TEST_PAUSE; |
| 54 | tst_tmpdir(); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 57 | int main(int ac, char **av) |
| 58 | { |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 59 | int ret; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 60 | int lc; |
Cyril Hrubis | 988aabf | 2014-08-20 17:33:21 +0200 | [diff] [blame] | 61 | int32_t ne_key; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 62 | const char *msg; |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 63 | |
Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 65 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 66 | |
| 67 | setup(); |
| 68 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 70 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 72 | |
| 73 | for (testno = 1; testno < TST_TOTAL; ++testno) { |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 74 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 75 | /* Call keyctl() and ask for a keyring's ID. */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 76 | ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | KEY_SPEC_USER_SESSION_KEYRING); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 78 | if (ret != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | tst_resm(TPASS, |
| 80 | "KEYCTL_GET_KEYRING_ID succeeded"); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 81 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | tst_resm(TFAIL | TERRNO, |
| 83 | "KEYCTL_GET_KEYRING_ID"); |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 87 | ret = ltp_syscall(__NR_keyctl, KEYCTL_READ, |
| 88 | ne_key); |
Garrett Cooper | da90805 | 2010-10-27 06:05:00 -0700 | [diff] [blame] | 89 | if (ret == -1 && errno == ENOKEY) |
| 90 | break; |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 91 | } |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 92 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 93 | /* Call keyctl. */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 94 | ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 95 | if (ret != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | tst_resm(TFAIL | TERRNO, |
| 97 | "KEYCTL_REVOKE succeeded unexpectedly"); |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 98 | } else { |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 99 | /* Check for the correct error num. */ |
yaberauneya | c2e8839 | 2009-11-24 20:36:37 +0000 | [diff] [blame] | 100 | if (errno == ENOKEY) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | tst_resm(TPASS | TERRNO, |
| 102 | "KEYCTL_REVOKE got expected " |
| 103 | "errno"); |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 104 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | tst_resm(TFAIL | TERRNO, |
| 106 | "KEYCTL_REVOKE got unexpected " |
| 107 | "errno"); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 108 | } |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 109 | |
yaberauneya | faf0748 | 2009-11-14 06:36:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | cleanup(); |
Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 116 | tst_exit(); |
subrata_modak | 5c8fc01 | 2009-05-21 18:17:32 +0000 | [diff] [blame] | 117 | } |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 118 | #else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | int main(void) |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 120 | { |
Cyril Hrubis | da7d01b | 2014-08-20 16:47:53 +0200 | [diff] [blame] | 121 | tst_brkm(TCONF, NULL, "keyctl syscall support not available on system"); |
Garrett Cooper | e99ebbc | 2010-11-03 10:15:10 -0700 | [diff] [blame] | 122 | } |
Cyril Hrubis | 988aabf | 2014-08-20 17:33:21 +0200 | [diff] [blame] | 123 | #endif /* HAVE_LINUX_KEYCTL_H */ |