[PATCH 1/2] KEYS: add user_key_payload_rcu()

From: Jan Stancek
Date: Wed Feb 22 2017 - 17:55:20 EST


user_key_payload() is wrapper for rcu_dereference_protected(),
and can't be used with just rcu_read_lock() held.

This patch adds user_key_payload_rcu() for accessing key
payload in RCU read-side section, without the need
to hold key semaphore.

Signed-off-by: Jan Stancek <jstancek@xxxxxxxxxx>
---
Documentation/security/keys.txt | 5 +++++
include/keys/user-type.h | 5 +++++
include/linux/key.h | 3 +++
3 files changed, 13 insertions(+)

diff --git a/Documentation/security/keys.txt b/Documentation/security/keys.txt
index 3849814bfe6d..da89a854edd2 100644
--- a/Documentation/security/keys.txt
+++ b/Documentation/security/keys.txt
@@ -1152,8 +1152,13 @@ access the data:
wrap the RCU calls to this element:

rcu_assign_keypointer(struct key *key, void *data);
+
+ /* access payload with semaphore held */
void *rcu_dereference_key(struct key *key);

+ /* access payload in RCU read-side section*/
+ void *rcu_read_dereference_key(struct key *key);
+

===================
DEFINING A KEY TYPE
diff --git a/include/keys/user-type.h b/include/keys/user-type.h
index c56fef40f53e..521bf3369904 100644
--- a/include/keys/user-type.h
+++ b/include/keys/user-type.h
@@ -53,6 +53,11 @@ static inline const struct user_key_payload *user_key_payload(const struct key *
return (struct user_key_payload *)rcu_dereference_key(key);
}

+static inline const struct user_key_payload *user_key_payload_rcu(const struct key *key)
+{
+ return (struct user_key_payload *)rcu_read_dereference_key(key);
+}
+
#endif /* CONFIG_KEYS */

#endif /* _KEYS_USER_TYPE_H */
diff --git a/include/linux/key.h b/include/linux/key.h
index 722914798f37..b6a8c5896761 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -358,6 +358,9 @@ static inline bool key_is_instantiated(const struct key *key)
(rcu_dereference_protected((KEY)->payload.rcu_data0, \
rwsem_is_locked(&((struct key *)(KEY))->sem)))

+#define rcu_read_dereference_key(KEY) \
+ (rcu_dereference((KEY)->payload.rcu_data0))
+
#define rcu_assign_keypointer(KEY, PAYLOAD) \
do { \
rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
--
1.8.3.1