Skip to content

Commit b0377fe

Browse files
author
Al Viro
committed
copy_{to,from}_user(): consolidate object size checks
... and move them into thread_info.h, next to check_object_size() Signed-off-by: Al Viro <[email protected]>
1 parent 9c5f690 commit b0377fe

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

include/linux/thread_info.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ static inline void check_object_size(const void *ptr, unsigned long n,
113113
{ }
114114
#endif /* CONFIG_HARDENED_USERCOPY */
115115

116+
extern void __compiletime_error("copy source size is too small")
117+
__bad_copy_from(void);
118+
extern void __compiletime_error("copy destination size is too small")
119+
__bad_copy_to(void);
120+
121+
static inline void copy_overflow(int size, unsigned long count)
122+
{
123+
WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
124+
}
125+
126+
static __always_inline bool
127+
check_copy_size(const void *addr, size_t bytes, bool is_source)
128+
{
129+
int sz = __compiletime_object_size(addr);
130+
if (unlikely(sz >= 0 && sz < bytes)) {
131+
if (!__builtin_constant_p(bytes))
132+
copy_overflow(sz, bytes);
133+
else if (is_source)
134+
__bad_copy_from();
135+
else
136+
__bad_copy_to();
137+
return false;
138+
}
139+
check_object_size(addr, bytes, is_source);
140+
return true;
141+
}
142+
116143
#ifndef arch_setup_new_exec
117144
static inline void arch_setup_new_exec(void) { }
118145
#endif

include/linux/uaccess.h

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,43 +139,19 @@ extern unsigned long
139139
_copy_to_user(void __user *, const void *, unsigned long);
140140
#endif
141141

142-
extern void __compiletime_error("usercopy buffer size is too small")
143-
__bad_copy_user(void);
144-
145-
static inline void copy_user_overflow(int size, unsigned long count)
146-
{
147-
WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
148-
}
149-
150142
static __always_inline unsigned long __must_check
151143
copy_from_user(void *to, const void __user *from, unsigned long n)
152144
{
153-
int sz = __compiletime_object_size(to);
154-
155-
if (likely(sz < 0 || sz >= n)) {
156-
check_object_size(to, n, false);
145+
if (likely(check_copy_size(to, n, false)))
157146
n = _copy_from_user(to, from, n);
158-
} else if (!__builtin_constant_p(n))
159-
copy_user_overflow(sz, n);
160-
else
161-
__bad_copy_user();
162-
163147
return n;
164148
}
165149

166150
static __always_inline unsigned long __must_check
167151
copy_to_user(void __user *to, const void *from, unsigned long n)
168152
{
169-
int sz = __compiletime_object_size(from);
170-
171-
if (likely(sz < 0 || sz >= n)) {
172-
check_object_size(from, n, true);
153+
if (likely(check_copy_size(from, n, true)))
173154
n = _copy_to_user(to, from, n);
174-
} else if (!__builtin_constant_p(n))
175-
copy_user_overflow(sz, n);
176-
else
177-
__bad_copy_user();
178-
179155
return n;
180156
}
181157
#ifdef CONFIG_COMPAT

0 commit comments

Comments
 (0)