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

Unified · Split

kernel/module/dups.c+40−16
@@ -30,6 +30,7 @@ · 29 unmodified lines
30#include <linux/ptrace.h>30#include <linux/ptrace.h>
31#include <linux/async.h>31#include <linux/async.h>
32#include <linux/uaccess.h>32#include <linux/uaccess.h>
33#include <linux/refcount.h>
33 34
34#include "internal.h"35#include "internal.h"
35 36
@@ -38,13 +39,12 @@ · 2 unmodified lines
38static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);39static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
39module_param(enable_dups_trace, bool_enable_only, 0644);40module_param(enable_dups_trace, bool_enable_only, 0644);
40 41
41/*42/* A mutex-protected list of active kmod requests. */
42 * Protects dup_kmod_reqs list, adds / removals with RCU.
43 */
44static DEFINE_MUTEX(kmod_dup_mutex);43static DEFINE_MUTEX(kmod_dup_mutex);
45static LIST_HEAD(dup_kmod_reqs);44static LIST_HEAD(dup_kmod_reqs);
46 45
47struct kmod_dup_req {46struct kmod_dup_req {
47 refcount_t refcount;
48 struct list_head list;48 struct list_head list;
49 char name[MODULE_NAME_LEN];49 char name[MODULE_NAME_LEN];
50 struct completion first_req_done;50 struct completion first_req_done;
@@ -52,12 +52,24 @@ struct kmod_dup_req { · 1 unmodified line
52 int dup_ret;52 int dup_ret;
53};53};
54 54
55static void get_kmod_req(struct kmod_dup_req *kmod_req)
56{
57 refcount_inc(&kmod_req->refcount);
58}
59
60static 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
55static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)66static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
56{67{
57 struct kmod_dup_req *kmod_req;68 struct kmod_dup_req *kmod_req;
58 69
59 list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list,70 lockdep_assert_held(&kmod_dup_mutex);
60 lockdep_is_held(&kmod_dup_mutex)) {71
72 list_for_each_entry(kmod_req, &dup_kmod_reqs, list) {
61 if (strlen(kmod_req->name) == strlen(module_name) &&73 if (strlen(kmod_req->name) == strlen(module_name) &&
62 !memcmp(kmod_req->name, module_name, strlen(module_name))) {74 !memcmp(kmod_req->name, module_name, strlen(module_name))) {
63 return kmod_req;75 return kmod_req;
@@ -86,10 +98,10 @@ static void kmod_dup_request_delete(struct work_struct *work) · 22 unmodified lines
86 * just returning 0.98 * just returning 0.
87 */99 */
88 mutex_lock(&kmod_dup_mutex);100 mutex_lock(&kmod_dup_mutex);
89 list_del_rcu(&kmod_req->list);101 list_del(&kmod_req->list);
90 synchronize_rcu();
91 mutex_unlock(&kmod_dup_mutex);102 mutex_unlock(&kmod_dup_mutex);
92 kfree(kmod_req);103
104 put_kmod_req(kmod_req);
93}105}
94 106
95bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)107bool 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 if (!new_kmod_req)117 if (!new_kmod_req)
106 return false;118 return false;
107 119
120 refcount_set(&new_kmod_req->refcount, 1);
108 strscpy(new_kmod_req->name, module_name);121 strscpy(new_kmod_req->name, module_name);
109 INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);122 INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete);
110 init_completion(&new_kmod_req->first_req_done);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 * keep tab on duplicates later.149 * keep tab on duplicates later.
137 */150 */
138 pr_debug("New request_module() for %s\n", module_name);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 mutex_unlock(&kmod_dup_mutex);153 mutex_unlock(&kmod_dup_mutex);
141 return false;154 return false;
142 }155 }
156
157 get_kmod_req(kmod_req);
143 mutex_unlock(&kmod_dup_mutex);158 mutex_unlock(&kmod_dup_mutex);
144 159
145 /* We are dealing with a duplicate request now */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 * calls bail out right away.184 * calls bail out right away.
170 */185 */
171 *dup_ret = 0;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 TASK_KILLABLE);199 TASK_KILLABLE);
185 if (ret) {200 if (ret) {
186 *dup_ret = ret;201 *dup_ret = ret;
187 return true;202 goto out;
188 }203 }
189 204
190 /* Now the duplicate request has the same exact return value as the first request */205 /* Now the duplicate request has the same exact return value as the first request */
191 *dup_ret = kmod_req->dup_ret;206 *dup_ret = kmod_req->dup_ret;
192 207
208out:
209 put_kmod_req(kmod_req);
193 return true;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 mutex_lock(&kmod_dup_mutex);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 kmod_req = kmod_dup_request_lookup(module_name);225 kmod_req = kmod_dup_request_lookup(module_name);
203 if (!kmod_req)226 if (!kmod_req || completion_done(&kmod_req->first_req_done)) {
204 goto out;227 mutex_unlock(&kmod_dup_mutex);
228 return;
229 }
205 230
206 kmod_req->dup_ret = ret;231 kmod_req->dup_ret = ret;
207 232
208 /* Inform all duplicate waiters to check the return value. */233 /* Inform all duplicate waiters to check the return value. */
209 complete_all(&kmod_req->first_req_done);234 complete_all(&kmod_req->first_req_done);
210 235
236 mutex_unlock(&kmod_dup_mutex);
237
211 /*238 /*
212 * Now that we have allowed prior request_module() calls to go on239 * Now that we have allowed prior request_module() calls to go on
213 * with life, let's schedule deleting this entry. We don't have240 * 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 * possible abuses of vmalloc() incurred by finit_module() thrashing.243 * possible abuses of vmalloc() incurred by finit_module() thrashing.
217 */244 */
218 queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ);245 queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ);
219
220out:
221 mutex_unlock(&kmod_dup_mutex);
222}246}

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