Skip to content

Commit 5c7e372

Browse files
thejhJames Morris
authored andcommitted
security: don't use RCU accessors for cred->session_keyring
sparse complains that a bunch of places in kernel/cred.c access cred->session_keyring without the RCU helpers required by the __rcu annotation. cred->session_keyring is written in the following places: - prepare_kernel_cred() [in a new cred struct] - keyctl_session_to_parent() [in a new cred struct] - prepare_creds [in a new cred struct, via memcpy] - install_session_keyring_to_cred() - from install_session_keyring() on new creds - from join_session_keyring() on new creds [twice] - from umh_keys_init() - from call_usermodehelper_exec_async() on new creds All of these writes are before the creds are committed; therefore, cred->session_keyring doesn't need RCU protection. Remove the __rcu annotation and fix up all existing users that use __rcu. Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: James Morris <james.morris@microsoft.com>
1 parent 1b26fcd commit 5c7e372

3 files changed

Lines changed: 7 additions & 16 deletions

File tree

‎include/linux/cred.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct cred {
138138
#ifdef CONFIG_KEYS
139139
unsigned char jit_keyring; /* default keyring to attach requested
140140
* keys to */
141-
struct key __rcu *session_keyring; /* keyring inherited over fork */
141+
struct key *session_keyring; /* keyring inherited over fork */
142142
struct key *process_keyring; /* keyring private to this process */
143143
struct key *thread_keyring; /* keyring private to this thread */
144144
struct key *request_key_auth; /* assumed request_key authority */

‎security/keys/process_keys.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ static int install_process_keyring(void)
227227
* Install the given keyring as the session keyring of the given credentials
228228
* struct, replacing the existing one if any. If the given keyring is NULL,
229229
* then install a new anonymous session keyring.
230+
* @cred can not be in use by any task yet.
230231
*
231232
* Return: 0 on success; -errno on failure.
232233
*/
@@ -254,7 +255,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
254255

255256
/* install the keyring */
256257
old = cred->session_keyring;
257-
rcu_assign_pointer(cred->session_keyring, keyring);
258+
cred->session_keyring = keyring;
258259

259260
if (old)
260261
key_put(old);
@@ -392,11 +393,8 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
392393

393394
/* search the session keyring */
394395
if (ctx->cred->session_keyring) {
395-
rcu_read_lock();
396396
key_ref = keyring_search_aux(
397-
make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
398-
ctx);
399-
rcu_read_unlock();
397+
make_key_ref(ctx->cred->session_keyring, 1), ctx);
400398

401399
if (!IS_ERR(key_ref))
402400
goto found;
@@ -612,10 +610,8 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
612610
goto reget_creds;
613611
}
614612

615-
rcu_read_lock();
616-
key = rcu_dereference(ctx.cred->session_keyring);
613+
key = ctx.cred->session_keyring;
617614
__key_get(key);
618-
rcu_read_unlock();
619615
key_ref = make_key_ref(key, 1);
620616
break;
621617

‎security/keys/request_key.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,10 @@ static int call_sbin_request_key(struct key *authkey, void *aux)
142142
prkey = cred->process_keyring->serial;
143143
sprintf(keyring_str[1], "%d", prkey);
144144

145-
rcu_read_lock();
146-
session = rcu_dereference(cred->session_keyring);
145+
session = cred->session_keyring;
147146
if (!session)
148147
session = cred->user->session_keyring;
149148
sskey = session->serial;
150-
rcu_read_unlock();
151149

152150
sprintf(keyring_str[2], "%d", sskey);
153151

@@ -287,10 +285,7 @@ static int construct_get_dest_keyring(struct key **_dest_keyring)
287285

288286
/* fall through */
289287
case KEY_REQKEY_DEFL_SESSION_KEYRING:
290-
rcu_read_lock();
291-
dest_keyring = key_get(
292-
rcu_dereference(cred->session_keyring));
293-
rcu_read_unlock();
288+
dest_keyring = key_get(cred->session_keyring);
294289

295290
if (dest_keyring)
296291
break;

0 commit comments

Comments
 (0)