• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Fujitsu Ltd.
4  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
5  * Copyright (c) Linux Test Project, 2017-2024
6  * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Basic request_key(2) failure checking. request_key(2) should return -1 and
13  * set expected errno:
14  *
15  * 1. ENOKEY (no matching key was found),
16  * 2. EKEYREVOKED (revoked key was found)
17  * 3. EKEYEXPIRED (expired key was found)
18  */
19 
20 #include <errno.h>
21 
22 #include "tst_test.h"
23 #include "lapi/keyctl.h"
24 
25 static int key1;
26 static int key2;
27 static int key3;
28 
29 static struct test_case {
30 	const char *des;
31 	int exp_err;
32 	int *id;
33 } tcases[] = {
34 	{"ltp1", ENOKEY, &key1},
35 	{"ltp2", EKEYREVOKED, &key2},
36 	{"ltp3", EKEYEXPIRED, &key3}
37 };
38 
verify_request_key(unsigned int n)39 static void verify_request_key(unsigned int n)
40 {
41 	struct test_case *tc = tcases + n;
42 
43 	TST_EXP_FAIL2(request_key("keyring", tc->des, NULL, *tc->id),
44 		      tc->exp_err, "request_key(\"keyring\", %s, NULL, %d)",
45 		      tc->des, *tc->id);
46 }
47 
init_key(char * name,int cmd)48 static int init_key(char *name, int cmd)
49 {
50 	int n;
51 	int sec = 1;
52 
53 	n = add_key("keyring", name, NULL, 0, KEY_SPEC_THREAD_KEYRING);
54 	if (n == -1)
55 		tst_brk(TBROK | TERRNO, "add_key() failed");
56 
57 	if (cmd == KEYCTL_REVOKE) {
58 		if (keyctl(cmd, n) == -1)
59 			tst_brk(TBROK | TERRNO,	"failed to revoke a key");
60 	}
61 
62 	if (cmd == KEYCTL_SET_TIMEOUT) {
63 		if (keyctl(cmd, n, sec) == -1) {
64 			tst_brk(TBROK | TERRNO,
65 				"failed to set timeout for a key");
66 		}
67 
68 		sleep(sec + 1);
69 	}
70 
71 	return n;
72 }
73 
setup(void)74 static void setup(void)
75 {
76 	key1 = KEY_REQKEY_DEFL_DEFAULT;
77 	key2 = init_key("ltp2", KEYCTL_REVOKE);
78 	key3 = init_key("ltp3", KEYCTL_SET_TIMEOUT);
79 }
80 
81 static struct tst_test test = {
82 	.setup = setup,
83 	.tcnt = ARRAY_SIZE(tcases),
84 	.test = verify_request_key,
85 };
86