The kmod dups code uses RCU to ensure that a kmod_dup_req instance is freed only after it is no longer referenced. When releasing an instance, the kmod_dup_request_delete() function removes the kmod_dup_req from the dup_kmod_reqs list, waits via synchronize_rcu() and finally frees it. However, this doesn't work correctly because parallel users referencing the instance in kmod_dup_request_exists_wait() don't enter an RCU read-side critical section. This can result in a use-after-free. The kmod_dup_request_exists_wait() function may need to hold a valid reference to a kmod_dup_req instance across a blocking wait until the corresponding modprobe command completes. This makes it unsuitable for RCU. Fix the issue by changing the lifecycle management of kmod_dup_req to use reference counting.
Unified · Split
| @@ -30,6 +30,7 @@ · 29 unmodified lines | ||
| 30 | 30 | #include <linux/ptrace.h> |
| 31 | 31 | #include <linux/async.h> |
| 32 | 32 | #include <linux/uaccess.h> |
| 33 | +#include <linux/refcount.h> | |
| 33 | 34 | |
| 34 | 35 | #include "internal.h" |
| 35 | 36 | |
| @@ -38,13 +39,12 @@ · 2 unmodified lines | ||
| 38 | 39 | static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE); |
| 39 | 40 | module_param(enable_dups_trace, bool_enable_only, 0644); |
| 40 | 41 | |
| 41 | −/* | |
| 42 | − * Protects dup_kmod_reqs list, adds / removals with RCU. | |
| 43 | − */ | |
| 42 | +/* A mutex-protected list of active kmod requests. */ | |
| 44 | 43 | static DEFINE_MUTEX(kmod_dup_mutex); |
| 45 | 44 | static LIST_HEAD(dup_kmod_reqs); |
| 46 | 45 | |
| 47 | 46 | struct kmod_dup_req { |
| 47 | + refcount_t refcount; | |
| 48 | 48 | struct list_head list; |
| 49 | 49 | char name[MODULE_NAME_LEN]; |
| 50 | 50 | struct completion first_req_done; |
| @@ -52,12 +52,24 @@ struct kmod_dup_req { · 1 unmodified line | ||
| 52 | 52 | int dup_ret; |
| 53 | 53 | }; |
| 54 | 54 | |
| 55 | +static void get_kmod_req(struct kmod_dup_req *kmod_req) | |
| 56 | +{ | |
| 57 | + refcount_inc(&kmod_req->refcount); | |
| 58 | +} | |
| 59 | + | |
| 60 | +static void put_kmod_req(struct kmod_dup_req *kmod_req) | |
| 61 | +{ | |
| 62 | + if (refcount_dec_and_test(&kmod_req->refcount)) | |
| 63 | + kfree(kmod_req); | |
| 64 | +} | |
| 65 | + | |
| 55 | 66 | static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name) |
| 56 | 67 | { |
| 57 | 68 | struct kmod_dup_req *kmod_req; |
| 58 | 69 | |
| 59 | − list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list, | |
| 60 | − lockdep_is_held(&kmod_dup_mutex)) { | |
| 70 | + lockdep_assert_held(&kmod_dup_mutex); | |
| 71 | + | |
| 72 | + list_for_each_entry(kmod_req, &dup_kmod_reqs, list) { | |
| 61 | 73 | if (strlen(kmod_req->name) == strlen(module_name) && |
| 62 | 74 | !memcmp(kmod_req->name, module_name, strlen(module_name))) { |
| 63 | 75 | return kmod_req; |
| @@ -86,10 +98,10 @@ static void kmod_dup_request_delete(struct work_struct *work) · 22 unmodified lines | ||
| 86 | 98 | * just returning 0. |
| 87 | 99 | */ |
| 88 | 100 | mutex_lock(&kmod_dup_mutex); |
| 89 | − list_del_rcu(&kmod_req->list); | |
| 90 | − synchronize_rcu(); | |
| 101 | + list_del(&kmod_req->list); | |
| 91 | 102 | mutex_unlock(&kmod_dup_mutex); |
| 92 | − kfree(kmod_req); | |
| 103 | + | |
| 104 | + put_kmod_req(kmod_req); | |
| 93 | 105 | } |
| 94 | 106 | |
| 95 | 107 | bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) |
| @@ -105,6 +117,7 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) · 9 unmodified lines | ||
| 105 | 117 | if (!new_kmod_req) |
| 106 | 118 | return false; |
| 107 | 119 | |
| 120 | + refcount_set(&new_kmod_req->refcount, 1); | |
| 108 | 121 | strscpy(new_kmod_req->name, module_name); |
| 109 | 122 | INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete); |
| 110 | 123 | init_completion(&new_kmod_req->first_req_done); |
| @@ -136,10 +149,12 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) · 25 unmodified lines | ||
| 136 | 149 | * keep tab on duplicates later. |
| 137 | 150 | */ |
| 138 | 151 | pr_debug("New request_module() for %s\n", module_name); |
| 139 | − list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs); | |
| 152 | + list_add(&new_kmod_req->list, &dup_kmod_reqs); | |
| 140 | 153 | mutex_unlock(&kmod_dup_mutex); |
| 141 | 154 | return false; |
| 142 | 155 | } |
| 156 | + | |
| 157 | + get_kmod_req(kmod_req); | |
| 143 | 158 | mutex_unlock(&kmod_dup_mutex); |
| 144 | 159 | |
| 145 | 160 | /* We are dealing with a duplicate request now */ |
| @@ -169,7 +184,7 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) · 23 unmodified lines | ||
| 169 | 184 | * calls bail out right away. |
| 170 | 185 | */ |
| 171 | 186 | *dup_ret = 0; |
| 172 | − return true; | |
| 187 | + goto out; | |
| 173 | 188 | } |
| 174 | 189 | |
| 175 | 190 | /* |
| @@ -184,12 +199,14 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) · 8 unmodified lines | ||
| 184 | 199 | TASK_KILLABLE); |
| 185 | 200 | if (ret) { |
| 186 | 201 | *dup_ret = ret; |
| 187 | − return true; | |
| 202 | + goto out; | |
| 188 | 203 | } |
| 189 | 204 | |
| 190 | 205 | /* Now the duplicate request has the same exact return value as the first request */ |
| 191 | 206 | *dup_ret = kmod_req->dup_ret; |
| 192 | 207 | |
| 208 | +out: | |
| 209 | + put_kmod_req(kmod_req); | |
| 193 | 210 | return true; |
| 194 | 211 | } |
| 195 | 212 | |
| @@ -199,15 +216,25 @@ void kmod_dup_request_announce(char *module_name, int ret) · 3 unmodified lines | ||
| 199 | 216 | |
| 200 | 217 | mutex_lock(&kmod_dup_mutex); |
| 201 | 218 | |
| 219 | + /* | |
| 220 | + * Look for a kmod_dup_req previously added in | |
| 221 | + * kmod_dup_request_exists_wait(). Note that a request_module_nowait() | |
| 222 | + * without its own kmod_dup_req entry can announce a result of | |
| 223 | + * a concurrent request_module() call. | |
| 224 | + */ | |
| 202 | 225 | kmod_req = kmod_dup_request_lookup(module_name); |
| 203 | − if (!kmod_req) | |
| 204 | − goto out; | |
| 226 | + if (!kmod_req || completion_done(&kmod_req->first_req_done)) { | |
| 227 | + mutex_unlock(&kmod_dup_mutex); | |
| 228 | + return; | |
| 229 | + } | |
| 205 | 230 | |
| 206 | 231 | kmod_req->dup_ret = ret; |
| 207 | 232 | |
| 208 | 233 | /* Inform all duplicate waiters to check the return value. */ |
| 209 | 234 | complete_all(&kmod_req->first_req_done); |
| 210 | 235 | |
| 236 | + mutex_unlock(&kmod_dup_mutex); | |
| 237 | + | |
| 211 | 238 | /* |
| 212 | 239 | * Now that we have allowed prior request_module() calls to go on |
| 213 | 240 | * with life, let's schedule deleting this entry. We don't have |
| @@ -216,7 +243,4 @@ void kmod_dup_request_announce(char *module_name, int ret) · 2 unmodified lines | ||
| 216 | 243 | * possible abuses of vmalloc() incurred by finit_module() thrashing. |
| 217 | 244 | */ |
| 218 | 245 | queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ); |
| 219 | − | |
| 220 | −out: | |
| 221 | − mutex_unlock(&kmod_dup_mutex); | |
| 222 | 246 | } |