[LTP] [PATCH v4 1/2] Add SAFE_KEYCTL macro

Andrea Cervesato andrea.cervesato@suse.de
Thu Aug 22 12:17:14 CEST 2024


From: Andrea Cervesato <andrea.cervesato@suse.com>

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 include/lapi/keyctl.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/include/lapi/keyctl.h b/include/lapi/keyctl.h
index 3be782494..e08b8f132 100644
--- a/include/lapi/keyctl.h
+++ b/include/lapi/keyctl.h
@@ -116,6 +116,10 @@ static inline key_serial_t keyctl_join_session_keyring(const char *name) {
 # define KEYCTL_SETPERM 5
 #endif
 
+#ifndef KEYCTL_DESCRIBE
+# define KEYCTL_DESCRIBE 6
+#endif
+
 #ifndef KEYCTL_CLEAR
 # define KEYCTL_CLEAR 7
 #endif
@@ -124,6 +128,10 @@ static inline key_serial_t keyctl_join_session_keyring(const char *name) {
 # define KEYCTL_UNLINK 9
 #endif
 
+#ifndef KEYCTL_SEARCH
+# define KEYCTL_SEARCH 10
+#endif
+
 #ifndef KEYCTL_READ
 # define KEYCTL_READ 11
 #endif
@@ -136,10 +144,26 @@ static inline key_serial_t keyctl_join_session_keyring(const char *name) {
 # define KEYCTL_SET_TIMEOUT 15
 #endif
 
+#ifndef KEYCTL_ASSUME_AUTHORITY
+# define KEYCTL_ASSUME_AUTHORITY 16
+#endif
+
+#ifndef KEYCTL_GET_SECURITY
+# define KEYCTL_GET_SECURITY 17
+#endif
+
 #ifndef KEYCTL_INVALIDATE
 # define KEYCTL_INVALIDATE 21
 #endif
 
+#ifndef KEYCTL_GET_PERSISTENT
+# define KEYCTL_GET_PERSISTENT 22
+#endif
+
+#ifndef KEYCTL_DH_COMPUTE
+# define KEYCTL_DH_COMPUTE 23
+#endif
+
 #ifndef KEYCTL_WATCH_KEY
 # define KEYCTL_WATCH_KEY 32
 #endif
@@ -179,4 +203,53 @@ static inline key_serial_t keyctl_join_session_keyring(const char *name) {
 # define KEY_OTH_ALL     0x0000003f
 #endif /* !KEY_POS_VIEW */
 
+static inline long safe_keyctl(const char *file, const int lineno,
+	int cmd, unsigned long arg2, unsigned long arg3,
+	unsigned long arg4, unsigned long arg5)
+{
+	long rval;
+	int failure = 0;
+
+	rval = keyctl(cmd, arg2, arg3, arg4, arg5);
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"keyctl(%d, %lu, %lu, %lu, %lu)",
+			cmd, arg2, arg3, arg4, arg5);
+	}
+
+	switch (cmd) {
+	case KEYCTL_GET_KEYRING_ID:
+	case KEYCTL_JOIN_SESSION_KEYRING:
+	case KEYCTL_DESCRIBE:
+	case KEYCTL_SEARCH:
+	case KEYCTL_READ:
+	case KEYCTL_SET_REQKEY_KEYRING:
+	case KEYCTL_GET_SECURITY:
+	case KEYCTL_GET_PERSISTENT:
+	case KEYCTL_DH_COMPUTE:
+		if (rval < 0)
+			failure = 1;
+		break;
+	case KEYCTL_ASSUME_AUTHORITY:
+		if ((!arg2 && rval) || (arg2 && rval < 0))
+			failure = 1;
+		break;
+	default:
+		if (rval)
+			failure = 1;
+		break;
+	}
+
+	if (failure) {
+		tst_brk_(file, lineno, TBROK,
+			"keyctl(%d, %lu, %lu, %lu, %lu) returned %ld",
+			cmd, arg2, arg3, arg4, arg5, rval);
+	}
+
+	return rval;
+}
+#define SAFE_KEYCTL(cmd, arg2, arg3, arg4, arg5) \
+	safe_keyctl(__FILE__, __LINE__, \
+	     (cmd), (arg2), (arg3), (arg4), (arg5))
+
 #endif	/* LAPI_KEYCTL_H__ */

-- 
2.43.0



More information about the ltp mailing list