History

module/dups: Fix use-after-free in kmod_dup_req lifetime handling

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.

Petr Pavlu committed 3 months ago

Commit 5eecb111 parent d258ed81 file · +40 −16Browse files

Showing the changes to kernel/module (1 of 1 file). Show all

Unified · Split

kernel/module/dups.c+40−16
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include <linux/ptrace.h>
3131 #include <linux/async.h>
3232 #include <linux/uaccess.h>
33+#include <linux/refcount.h>
3334
3435 #include "internal.h"
3536
@@ -38,13 +39,12 @@ · 2 unmodified lines
3839 static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
3940 module_param(enable_dups_trace, bool_enable_only, 0644);
4041
41−/*
42− * Protects dup_kmod_reqs list, adds / removals with RCU.
43− */
42+/* A mutex-protected list of active kmod requests. */
4443 static DEFINE_MUTEX(kmod_dup_mutex);
4544 static LIST_HEAD(dup_kmod_reqs);
4645
4746 struct kmod_dup_req {
47+ refcount_t refcount;
4848 struct list_head list;
4949 char name[MODULE_NAME_LEN];
5050 struct completion first_req_done;
@@ -52,12 +52,24 @@ struct kmod_dup_req { · 1 unmodified line
5252 int dup_ret;
5353 };
5454
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+
5566 static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
5667 {
5768 struct kmod_dup_req *kmod_req;
5869
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) {
6173 if (strlen(kmod_req->name) == strlen(module_name) &&
6274 !memcmp(kmod_req->name, module_name, strlen(module_name))) {
6375 return kmod_req;
@@ -86,10 +98,10 @@ static void kmod_dup_request_delete(struct work_struct *work) · 22 unmodified lines
8698 * just returning 0.
8799 */
88100 mutex_lock(&kmod_dup_mutex);
89− list_del_rcu(&kmod_req->list);
90− synchronize_rcu();
101+ list_del(&kmod_req->list);
91102 mutex_unlock(&kmod_dup_mutex);
92− kfree(kmod_req);
103+
104+ put_kmod_req(kmod_req);
93105 }
94106
95107 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
105117 if (!new_kmod_req)
106118 return false;
107119
120+ refcount_set(&new_kmod_req->refcount, 1);
108121 strscpy(new_kmod_req->name, module_name);
109122 INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);
110123 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
136149 * keep tab on duplicates later.
137150 */
138151 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);
140153 mutex_unlock(&kmod_dup_mutex);
141154 return false;
142155 }
156+
157+ get_kmod_req(kmod_req);
143158 mutex_unlock(&kmod_dup_mutex);
144159
145160 /* 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
169184 * calls bail out right away.
170185 */
171186 *dup_ret = 0;
172− return true;
187+ goto out;
173188 }
174189
175190 /*
@@ -184,12 +199,14 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) · 8 unmodified lines
184199 TASK_KILLABLE);
185200 if (ret) {
186201 *dup_ret = ret;
187− return true;
202+ goto out;
188203 }
189204
190205 /* Now the duplicate request has the same exact return value as the first request */
191206 *dup_ret = kmod_req->dup_ret;
192207
208+out:
209+ put_kmod_req(kmod_req);
193210 return true;
194211 }
195212
@@ -199,15 +216,25 @@ void kmod_dup_request_announce(char *module_name, int ret) · 3 unmodified lines
199216
200217 mutex_lock(&kmod_dup_mutex);
201218
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+ */
202225 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+ }
205230
206231 kmod_req->dup_ret = ret;
207232
208233 /* Inform all duplicate waiters to check the return value. */
209234 complete_all(&kmod_req->first_req_done);
210235
236+ mutex_unlock(&kmod_dup_mutex);
237+
211238 /*
212239 * Now that we have allowed prior request_module() calls to go on
213240 * 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
216243 * possible abuses of vmalloc() incurred by finit_module() thrashing.
217244 */
218245 queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ);
219−
220−out:
221− mutex_unlock(&kmod_dup_mutex);
222246 }

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