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.
Unified · Split
| @@ -138,7 +138,7 @@ struct cred { · 137 unmodified lines | ||
| 138 | 138 | #ifdef CONFIG_KEYS |
| 139 | 139 | unsigned char jit_keyring; /* default keyring to attach requested |
| 140 | 140 | * keys to */ |
| 141 | − struct key __rcu *session_keyring; /* keyring inherited over fork */ | |
| 141 | + struct key *session_keyring; /* keyring inherited over fork */ | |
| 142 | 142 | struct key *process_keyring; /* keyring private to this process */ |
| 143 | 143 | struct key *thread_keyring; /* keyring private to this thread */ |
| 144 | 144 | struct key *request_key_auth; /* assumed request_key authority */ |
| @@ -227,6 +227,7 @@ static int install_process_keyring(void) · 226 unmodified lines | ||
| 227 | 227 | * Install the given keyring as the session keyring of the given credentials |
| 228 | 228 | * struct, replacing the existing one if any. If the given keyring is NULL, |
| 229 | 229 | * then install a new anonymous session keyring. |
| 230 | + * @cred can not be in use by any task yet. | |
| 230 | 231 | * |
| 231 | 232 | * Return: 0 on success; -errno on failure. |
| 232 | 233 | */ |
| @@ -254,7 +255,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring) · 21 unmodified lines | ||
| 254 | 255 | |
| 255 | 256 | /* install the keyring */ |
| 256 | 257 | old = cred->session_keyring; |
| 257 | − rcu_assign_pointer(cred->session_keyring, keyring); | |
| 258 | + cred->session_keyring = keyring; | |
| 258 | 259 | |
| 259 | 260 | if (old) |
| 260 | 261 | key_put(old); |
| @@ -392,11 +393,8 @@ key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) · 131 unmodified lines | ||
| 392 | 393 | |
| 393 | 394 | /* search the session keyring */ |
| 394 | 395 | if (ctx->cred->session_keyring) { |
| 395 | − rcu_read_lock(); | |
| 396 | 396 | 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); | |
| 400 | 398 | |
| 401 | 399 | if (!IS_ERR(key_ref)) |
| 402 | 400 | goto found; |
| @@ -612,10 +610,8 @@ try_again: · 209 unmodified lines | ||
| 612 | 610 | goto reget_creds; |
| 613 | 611 | } |
| 614 | 612 | |
| 615 | − rcu_read_lock(); | |
| 616 | − key = rcu_dereference(ctx.cred->session_keyring); | |
| 613 | + key = ctx.cred->session_keyring; | |
| 617 | 614 | __key_get(key); |
| 618 | − rcu_read_unlock(); | |
| 619 | 615 | key_ref = make_key_ref(key, 1); |
| 620 | 616 | break; |
| 621 | 617 | |
| @@ -142,12 +142,10 @@ static int call_sbin_request_key(struct key *authkey, void *aux) · 141 unmodified lines | ||
| 142 | 142 | prkey = cred->process_keyring->serial; |
| 143 | 143 | sprintf(keyring_str[1], "%d", prkey); |
| 144 | 144 | |
| 145 | − rcu_read_lock(); | |
| 146 | − session = rcu_dereference(cred->session_keyring); | |
| 145 | + session = cred->session_keyring; | |
| 147 | 146 | if (!session) |
| 148 | 147 | session = cred->user->session_keyring; |
| 149 | 148 | sskey = session->serial; |
| 150 | − rcu_read_unlock(); | |
| 151 | 149 | |
| 152 | 150 | sprintf(keyring_str[2], "%d", sskey); |
| 153 | 151 | |
| @@ -287,10 +285,7 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 133 unmodified lines | ||
| 287 | 285 | |
| 288 | 286 | /* fall through */ |
| 289 | 287 | 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); | |
| 294 | 289 | |
| 295 | 290 | if (dest_keyring) |
| 296 | 291 | break; |