History

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.

Jann Horn committed 7 years ago

Commit 5c7e3721 parent 1b26fcd3 files · +7 −16Browse files

Unified · Split

3 files
  1. include/linux/cred.h +1 −1
  2. security/keys/process_keys.c +4 −8
  3. security/keys/request_key.c +2 −7
include/linux/cred.h+1−1
@@ -138,7 +138,7 @@ struct cred { · 137 unmodified lines
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+4−8
@@ -227,6 +227,7 @@ static int install_process_keyring(void) · 226 unmodified lines
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) · 21 unmodified lines
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) · 131 unmodified lines
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 @@ try_again: · 209 unmodified lines
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+2−7
@@ -142,12 +142,10 @@ static int call_sbin_request_key(struct key *authkey, void *aux) · 141 unmodified lines
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) · 133 unmodified lines
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;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t