Skip to content

Commit 30306f6

Browse files
committed
Merge tag 'hardening-v5.19-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull hardening fixes from Kees Cook: - Correctly handle vm_map areas in hardened usercopy (Matthew Wilcox) - Adjust CFI RCU usage to avoid boot splats with cpuidle (Sami Tolvanen) * tag 'hardening-v5.19-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: usercopy: Make usercopy resilient against ridiculously large copies usercopy: Cast pointer to an integer once usercopy: Handle vm_map_ram() areas cfi: Fix __cfi_slowpath_diag RCU usage with cpuidle
2 parents afe9eb1 + 1dfbe9f commit 30306f6

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

include/linux/vmalloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size,
215215
void free_vm_area(struct vm_struct *area);
216216
extern struct vm_struct *remove_vm_area(const void *addr);
217217
extern struct vm_struct *find_vm_area(const void *addr);
218+
struct vmap_area *find_vmap_area(unsigned long addr);
218219

219220
static inline bool is_vm_area_hugepages(const void *addr)
220221
{

kernel/cfi.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ static inline cfi_check_fn find_module_check_fn(unsigned long ptr)
281281
static inline cfi_check_fn find_check_fn(unsigned long ptr)
282282
{
283283
cfi_check_fn fn = NULL;
284+
unsigned long flags;
285+
bool rcu_idle;
284286

285287
if (is_kernel_text(ptr))
286288
return __cfi_check;
@@ -290,13 +292,21 @@ static inline cfi_check_fn find_check_fn(unsigned long ptr)
290292
* the shadow and __module_address use RCU, so we need to wake it
291293
* up if necessary.
292294
*/
293-
RCU_NONIDLE({
294-
if (IS_ENABLED(CONFIG_CFI_CLANG_SHADOW))
295-
fn = find_shadow_check_fn(ptr);
295+
rcu_idle = !rcu_is_watching();
296+
if (rcu_idle) {
297+
local_irq_save(flags);
298+
rcu_irq_enter();
299+
}
300+
301+
if (IS_ENABLED(CONFIG_CFI_CLANG_SHADOW))
302+
fn = find_shadow_check_fn(ptr);
303+
if (!fn)
304+
fn = find_module_check_fn(ptr);
296305

297-
if (!fn)
298-
fn = find_module_check_fn(ptr);
299-
});
306+
if (rcu_idle) {
307+
rcu_irq_exit();
308+
local_irq_restore(flags);
309+
}
300310

301311
return fn;
302312
}

mm/usercopy.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,27 @@ static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
161161
static inline void check_heap_object(const void *ptr, unsigned long n,
162162
bool to_user)
163163
{
164+
uintptr_t addr = (uintptr_t)ptr;
165+
unsigned long offset;
164166
struct folio *folio;
165167

166168
if (is_kmap_addr(ptr)) {
167-
unsigned long page_end = (unsigned long)ptr | (PAGE_SIZE - 1);
168-
169-
if ((unsigned long)ptr + n - 1 > page_end)
170-
usercopy_abort("kmap", NULL, to_user,
171-
offset_in_page(ptr), n);
169+
offset = offset_in_page(ptr);
170+
if (n > PAGE_SIZE - offset)
171+
usercopy_abort("kmap", NULL, to_user, offset, n);
172172
return;
173173
}
174174

175175
if (is_vmalloc_addr(ptr)) {
176-
struct vm_struct *area = find_vm_area(ptr);
177-
unsigned long offset;
176+
struct vmap_area *area = find_vmap_area(addr);
178177

179-
if (!area) {
178+
if (!area)
180179
usercopy_abort("vmalloc", "no area", to_user, 0, n);
181-
return;
182-
}
183180

184-
offset = ptr - area->addr;
185-
if (offset + n > get_vm_area_size(area))
181+
if (n > area->va_end - addr) {
182+
offset = addr - area->va_start;
186183
usercopy_abort("vmalloc", NULL, to_user, offset, n);
184+
}
187185
return;
188186
}
189187

@@ -196,8 +194,8 @@ static inline void check_heap_object(const void *ptr, unsigned long n,
196194
/* Check slab allocator for flags and size. */
197195
__check_heap_object(ptr, n, folio_slab(folio), to_user);
198196
} else if (folio_test_large(folio)) {
199-
unsigned long offset = ptr - folio_address(folio);
200-
if (offset + n > folio_size(folio))
197+
offset = ptr - folio_address(folio);
198+
if (n > folio_size(folio) - offset)
201199
usercopy_abort("page alloc", NULL, to_user, offset, n);
202200
}
203201
}

mm/vmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)
17981798
free_vmap_area_noflush(va);
17991799
}
18001800

1801-
static struct vmap_area *find_vmap_area(unsigned long addr)
1801+
struct vmap_area *find_vmap_area(unsigned long addr)
18021802
{
18031803
struct vmap_area *va;
18041804

0 commit comments

Comments
 (0)