Skip to content

Commit 8f3e474

Browse files
0x7f454c46KAGA-KOKO
authored andcommitted
x86/mm: Add task_size parameter to mmap_base()
To correctly handle 32-bit and 64-bit mmap() syscalls in 64bit applications its required to have separate address bases to place a mapping. The tasksize can be used as an indicator to select the proper parameters for mmap_base(). This requires the following changes: - Add task_size argument to mmap_base() and make the calculation based on it. - Provide mmap_legacy_base() as a seperate function - Use the new functions in arch_pick_mmap_layout() [ tglx: Massaged changelog ] Signed-off-by: Dmitry Safonov <[email protected]> Cc: [email protected] Cc: [email protected] Cc: Andy Lutomirski <[email protected]> Cc: Cyrill Gorcunov <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: "Kirill A. Shutemov" <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 6a0b41d commit 8f3e474

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

arch/x86/include/asm/elf.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,19 @@ do { \
293293
} \
294294
} while (0)
295295

296+
/*
297+
* True on X86_32 or when emulating IA32 on X86_64
298+
*/
299+
static inline int mmap_is_ia32(void)
300+
{
301+
return IS_ENABLED(CONFIG_X86_32) ||
302+
(IS_ENABLED(CONFIG_COMPAT) &&
303+
test_thread_flag(TIF_ADDR32));
304+
}
305+
296306
#ifdef CONFIG_X86_32
297307

308+
#define __STACK_RND_MASK(is32bit) (0x7ff)
298309
#define STACK_RND_MASK (0x7ff)
299310

300311
#define ARCH_DLINFO ARCH_DLINFO_IA32
@@ -304,7 +315,8 @@ do { \
304315
#else /* CONFIG_X86_32 */
305316

306317
/* 1GB for 64bit, 8MB for 32bit */
307-
#define STACK_RND_MASK (test_thread_flag(TIF_ADDR32) ? 0x7ff : 0x3fffff)
318+
#define __STACK_RND_MASK(is32bit) ((is32bit) ? 0x7ff : 0x3fffff)
319+
#define STACK_RND_MASK __STACK_RND_MASK(mmap_is_ia32())
308320

309321
#define ARCH_DLINFO \
310322
do { \
@@ -348,16 +360,6 @@ extern int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
348360
int uses_interp);
349361
#define compat_arch_setup_additional_pages compat_arch_setup_additional_pages
350362

351-
/*
352-
* True on X86_32 or when emulating IA32 on X86_64
353-
*/
354-
static inline int mmap_is_ia32(void)
355-
{
356-
return IS_ENABLED(CONFIG_X86_32) ||
357-
(IS_ENABLED(CONFIG_COMPAT) &&
358-
test_thread_flag(TIF_ADDR32));
359-
}
360-
361363
/* Do not change the values. See get_align_mask() */
362364
enum align_flags {
363365
ALIGN_VA_32 = BIT(0),

arch/x86/include/asm/processor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ static inline void spin_lock_prefetch(const void *x)
797797
/*
798798
* User space process size: 3GB (default).
799799
*/
800+
#define IA32_PAGE_OFFSET PAGE_OFFSET
800801
#define TASK_SIZE PAGE_OFFSET
801802
#define TASK_SIZE_MAX TASK_SIZE
802803
#define STACK_TOP TASK_SIZE
@@ -873,7 +874,8 @@ extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
873874
* This decides where the kernel will search for a free chunk of vm
874875
* space during mmap's.
875876
*/
876-
#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
877+
#define __TASK_UNMAPPED_BASE(task_size) (PAGE_ALIGN(task_size / 3))
878+
#define TASK_UNMAPPED_BASE __TASK_UNMAPPED_BASE(TASK_SIZE)
877879

878880
#define KSTK_EIP(task) (task_pt_regs(task)->ip)
879881

arch/x86/mm/mmap.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ struct va_alignment __read_mostly va_align = {
3636
.flags = -1,
3737
};
3838

39-
static unsigned long stack_maxrandom_size(void)
39+
static inline unsigned long tasksize_32bit(void)
40+
{
41+
return IA32_PAGE_OFFSET;
42+
}
43+
44+
static unsigned long stack_maxrandom_size(unsigned long task_size)
4045
{
4146
unsigned long max = 0;
4247
if ((current->flags & PF_RANDOMIZE) &&
4348
!(current->personality & ADDR_NO_RANDOMIZE)) {
44-
max = ((-1UL) & STACK_RND_MASK) << PAGE_SHIFT;
49+
max = (-1UL) & __STACK_RND_MASK(task_size == tasksize_32bit());
50+
max <<= PAGE_SHIFT;
4551
}
4652

4753
return max;
4854
}
4955

50-
/*
51-
* Top of mmap area (just below the process stack).
52-
*
53-
* Leave an at least ~128 MB hole with possible stack randomization.
54-
*/
55-
#define MIN_GAP (128*1024*1024UL + stack_maxrandom_size())
56-
#define MAX_GAP (TASK_SIZE/6*5)
57-
5856
#ifdef CONFIG_COMPAT
5957
# define mmap32_rnd_bits mmap_rnd_compat_bits
6058
# define mmap64_rnd_bits mmap_rnd_bits
@@ -63,6 +61,8 @@ static unsigned long stack_maxrandom_size(void)
6361
# define mmap64_rnd_bits mmap_rnd_bits
6462
#endif
6563

64+
#define SIZE_128M (128 * 1024 * 1024UL)
65+
6666
static int mmap_is_legacy(void)
6767
{
6868
if (current->personality & ADDR_COMPAT_LAYOUT)
@@ -84,16 +84,30 @@ unsigned long arch_mmap_rnd(void)
8484
return arch_rnd(mmap_is_ia32() ? mmap32_rnd_bits : mmap64_rnd_bits);
8585
}
8686

87-
static unsigned long mmap_base(unsigned long rnd)
87+
static unsigned long mmap_base(unsigned long rnd, unsigned long task_size)
8888
{
8989
unsigned long gap = rlimit(RLIMIT_STACK);
90+
unsigned long gap_min, gap_max;
91+
92+
/*
93+
* Top of mmap area (just below the process stack).
94+
* Leave an at least ~128 MB hole with possible stack randomization.
95+
*/
96+
gap_min = SIZE_128M + stack_maxrandom_size(task_size);
97+
gap_max = (task_size / 6) * 5;
9098

91-
if (gap < MIN_GAP)
92-
gap = MIN_GAP;
93-
else if (gap > MAX_GAP)
94-
gap = MAX_GAP;
99+
if (gap < gap_min)
100+
gap = gap_min;
101+
else if (gap > gap_max)
102+
gap = gap_max;
95103

96-
return PAGE_ALIGN(TASK_SIZE - gap - rnd);
104+
return PAGE_ALIGN(task_size - gap - rnd);
105+
}
106+
107+
static unsigned long mmap_legacy_base(unsigned long rnd,
108+
unsigned long task_size)
109+
{
110+
return __TASK_UNMAPPED_BASE(task_size) + rnd;
97111
}
98112

99113
/*
@@ -107,13 +121,13 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
107121
if (current->flags & PF_RANDOMIZE)
108122
random_factor = arch_mmap_rnd();
109123

110-
mm->mmap_legacy_base = TASK_UNMAPPED_BASE + random_factor;
124+
mm->mmap_legacy_base = mmap_legacy_base(random_factor, TASK_SIZE);
111125

112126
if (mmap_is_legacy()) {
113127
mm->mmap_base = mm->mmap_legacy_base;
114128
mm->get_unmapped_area = arch_get_unmapped_area;
115129
} else {
116-
mm->mmap_base = mmap_base(random_factor);
130+
mm->mmap_base = mmap_base(random_factor, TASK_SIZE);
117131
mm->get_unmapped_area = arch_get_unmapped_area_topdown;
118132
}
119133
}

0 commit comments

Comments
 (0)