blob: f45424f663470a1e9569060ba1a8bb095572f419 [file] [log] [blame]
Cyril Hrubisda7d01b2014-08-20 16:47:53 +02001/******************************************************************************
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_modak5c8fc012009-05-21 18:17:32 +000026
Garrett Cooper020a4da2010-11-03 20:57:21 -070027#include "config.h"
Garrett Cooper6340c2a2010-11-01 00:16:19 -070028#include <sys/types.h>
Garrett Cooperda908052010-10-27 06:05:00 -070029#include <errno.h>
30#include <limits.h>
31#include <stdio.h>
Cyril Hrubis988aabf2014-08-20 17:33:21 +020032#include <stdint.h>
33#ifdef HAVE_LINUX_KEYCTL_H
34# include <linux/keyctl.h>
35#endif
Garrett Cooper4e772642010-10-31 20:58:03 -070036
subrata_modak5c8fc012009-05-21 18:17:32 +000037#include "test.h"
subrata_modak5c8fc012009-05-21 18:17:32 +000038#include "linux_syscall_numbers.h"
39
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020040char *TCID = "keyctl01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080041int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020042int TST_TOTAL = 2;
subrata_modak5c8fc012009-05-21 18:17:32 +000043
Cyril Hrubis988aabf2014-08-20 17:33:21 +020044#ifdef HAVE_LINUX_KEYCTL_H
Garrett Cooper2c282152010-12-16 00:55:50 -080045
Cyril Hrubisda7d01b2014-08-20 16:47:53 +020046static void cleanup(void)
47{
yaberauneyafaf07482009-11-14 06:36:06 +000048 tst_rmdir();
subrata_modak5c8fc012009-05-21 18:17:32 +000049}
50
Cyril Hrubisda7d01b2014-08-20 16:47:53 +020051static void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080052{
yaberauneyafaf07482009-11-14 06:36:06 +000053 TEST_PAUSE;
54 tst_tmpdir();
subrata_modak5c8fc012009-05-21 18:17:32 +000055}
56
Wanlong Gao354ebb42012-12-07 10:10:04 +080057int main(int ac, char **av)
58{
yaberauneyac2e88392009-11-24 20:36:37 +000059 int ret;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020060 int lc;
Cyril Hrubis988aabf2014-08-20 17:33:21 +020061 int32_t ne_key;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
yaberauneyafaf07482009-11-14 06:36:06 +000063
Cyril Hrubisda7d01b2014-08-20 16:47:53 +020064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080065 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
yaberauneyafaf07482009-11-14 06:36:06 +000066
67 setup();
68
yaberauneyafaf07482009-11-14 06:36:06 +000069 for (lc = 0; TEST_LOOPING(lc); lc++) {
70
Caspar Zhangd59a6592013-03-07 14:59:12 +080071 tst_count = 0;
yaberauneyafaf07482009-11-14 06:36:06 +000072
73 for (testno = 1; testno < TST_TOTAL; ++testno) {
Garrett Cooperda908052010-10-27 06:05:00 -070074
yaberauneyafaf07482009-11-14 06:36:06 +000075 /* Call keyctl() and ask for a keyring's ID. */
Jan Stancek359980f2013-02-15 10:16:05 +010076 ret = ltp_syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID,
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 KEY_SPEC_USER_SESSION_KEYRING);
yaberauneyac2e88392009-11-24 20:36:37 +000078 if (ret != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tst_resm(TPASS,
80 "KEYCTL_GET_KEYRING_ID succeeded");
yaberauneyafaf07482009-11-14 06:36:06 +000081 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 tst_resm(TFAIL | TERRNO,
83 "KEYCTL_GET_KEYRING_ID");
Garrett Cooperda908052010-10-27 06:05:00 -070084 }
85
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 for (ne_key = INT32_MAX; ne_key > INT32_MIN; ne_key--) {
Jan Stancek359980f2013-02-15 10:16:05 +010087 ret = ltp_syscall(__NR_keyctl, KEYCTL_READ,
88 ne_key);
Garrett Cooperda908052010-10-27 06:05:00 -070089 if (ret == -1 && errno == ENOKEY)
90 break;
yaberauneyafaf07482009-11-14 06:36:06 +000091 }
subrata_modak5c8fc012009-05-21 18:17:32 +000092
yaberauneyafaf07482009-11-14 06:36:06 +000093 /* Call keyctl. */
Jan Stancek359980f2013-02-15 10:16:05 +010094 ret = ltp_syscall(__NR_keyctl, KEYCTL_REVOKE, ne_key);
yaberauneyac2e88392009-11-24 20:36:37 +000095 if (ret != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 tst_resm(TFAIL | TERRNO,
97 "KEYCTL_REVOKE succeeded unexpectedly");
yaberauneyac2e88392009-11-24 20:36:37 +000098 } else {
yaberauneyafaf07482009-11-14 06:36:06 +000099 /* Check for the correct error num. */
yaberauneyac2e88392009-11-24 20:36:37 +0000100 if (errno == ENOKEY) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 tst_resm(TPASS | TERRNO,
102 "KEYCTL_REVOKE got expected "
103 "errno");
yaberauneyafaf07482009-11-14 06:36:06 +0000104 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 tst_resm(TFAIL | TERRNO,
106 "KEYCTL_REVOKE got unexpected "
107 "errno");
subrata_modak5c8fc012009-05-21 18:17:32 +0000108 }
subrata_modak5c8fc012009-05-21 18:17:32 +0000109
yaberauneyafaf07482009-11-14 06:36:06 +0000110 }
111
112 }
113
114 }
115 cleanup();
Cyril Hrubisda7d01b2014-08-20 16:47:53 +0200116 tst_exit();
subrata_modak5c8fc012009-05-21 18:17:32 +0000117}
Garrett Coopere99ebbc2010-11-03 10:15:10 -0700118#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119int main(void)
Garrett Coopere99ebbc2010-11-03 10:15:10 -0700120{
Cyril Hrubisda7d01b2014-08-20 16:47:53 +0200121 tst_brkm(TCONF, NULL, "keyctl syscall support not available on system");
Garrett Coopere99ebbc2010-11-03 10:15:10 -0700122}
Cyril Hrubis988aabf2014-08-20 17:33:21 +0200123#endif /* HAVE_LINUX_KEYCTL_H */