Skip to content

Commit b1d0a5d

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: x_tables: add and use xt_check_proc_name
recent and hashlimit both create /proc files, but only check that name is 0 terminated. This can trigger WARN() from procfs when name is "" or "/". Add helper for this and then use it for both. Cc: Eric Dumazet <eric.dumazet@gmail.com> Reported-by: Eric Dumazet <eric.dumazet@gmail.com> Reported-by: <syzbot+0502b00edac2a0680b61@syzkaller.appspotmail.com> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 932909d commit b1d0a5d

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ unsigned int *xt_alloc_entry_offsets(unsigned int size);
285285
bool xt_find_jump_offset(const unsigned int *offsets,
286286
unsigned int target, unsigned int size);
287287

288+
int xt_check_proc_name(const char *name, unsigned int size);
289+
288290
int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
289291
bool inv_proto);
290292
int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,

‎net/netfilter/x_tables.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,36 @@ textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
423423
return buf;
424424
}
425425

426+
/**
427+
* xt_check_proc_name - check that name is suitable for /proc file creation
428+
*
429+
* @name: file name candidate
430+
* @size: length of buffer
431+
*
432+
* some x_tables modules wish to create a file in /proc.
433+
* This function makes sure that the name is suitable for this
434+
* purpose, it checks that name is NUL terminated and isn't a 'special'
435+
* name, like "..".
436+
*
437+
* returns negative number on error or 0 if name is useable.
438+
*/
439+
int xt_check_proc_name(const char *name, unsigned int size)
440+
{
441+
if (name[0] == '\0')
442+
return -EINVAL;
443+
444+
if (strnlen(name, size) == size)
445+
return -ENAMETOOLONG;
446+
447+
if (strcmp(name, ".") == 0 ||
448+
strcmp(name, "..") == 0 ||
449+
strchr(name, '/'))
450+
return -EINVAL;
451+
452+
return 0;
453+
}
454+
EXPORT_SYMBOL(xt_check_proc_name);
455+
426456
int xt_check_match(struct xt_mtchk_param *par,
427457
unsigned int size, u_int8_t proto, bool inv_proto)
428458
{

‎net/netfilter/xt_hashlimit.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,9 @@ static int hashlimit_mt_check_v1(const struct xt_mtchk_param *par)
917917
struct hashlimit_cfg3 cfg = {};
918918
int ret;
919919

920-
if (info->name[sizeof(info->name) - 1] != '\0')
921-
return -EINVAL;
920+
ret = xt_check_proc_name(info->name, sizeof(info->name));
921+
if (ret)
922+
return ret;
922923

923924
ret = cfg_copy(&cfg, (void *)&info->cfg, 1);
924925

@@ -935,8 +936,9 @@ static int hashlimit_mt_check_v2(const struct xt_mtchk_param *par)
935936
struct hashlimit_cfg3 cfg = {};
936937
int ret;
937938

938-
if (info->name[sizeof(info->name) - 1] != '\0')
939-
return -EINVAL;
939+
ret = xt_check_proc_name(info->name, sizeof(info->name));
940+
if (ret)
941+
return ret;
940942

941943
ret = cfg_copy(&cfg, (void *)&info->cfg, 2);
942944

@@ -950,9 +952,11 @@ static int hashlimit_mt_check_v2(const struct xt_mtchk_param *par)
950952
static int hashlimit_mt_check(const struct xt_mtchk_param *par)
951953
{
952954
struct xt_hashlimit_mtinfo3 *info = par->matchinfo;
955+
int ret;
953956

954-
if (info->name[sizeof(info->name) - 1] != '\0')
955-
return -EINVAL;
957+
ret = xt_check_proc_name(info->name, sizeof(info->name));
958+
if (ret)
959+
return ret;
956960

957961
return hashlimit_mt_check_common(par, &info->hinfo, &info->cfg,
958962
info->name, 3);

‎net/netfilter/xt_recent.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,9 @@ static int recent_mt_check(const struct xt_mtchk_param *par,
361361
info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
362362
return -EINVAL;
363363
}
364-
if (info->name[0] == '\0' ||
365-
strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
366-
return -EINVAL;
364+
ret = xt_check_proc_name(info->name, sizeof(info->name));
365+
if (ret)
366+
return ret;
367367

368368
if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot)
369369
nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1;

0 commit comments

Comments
 (0)