Skip to content

Commit 70d55b6

Browse files
Florian Westphalbwhacks
authored andcommitted
netfilter: x_tables: add and use xt_check_proc_name
commit b1d0a5d upstream. 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> [bwh: Backported to 3.16: - xt_hashlimit has only one check function - Adjust context] Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
1 parent c9b4d8c commit 70d55b6

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

include/linux/netfilter/x_tables.h

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

250+
int xt_check_proc_name(const char *name, unsigned int size);
251+
250252
int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
251253
bool inv_proto);
252254
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
@@ -380,6 +380,36 @@ textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
380380
return buf;
381381
}
382382

383+
/**
384+
* xt_check_proc_name - check that name is suitable for /proc file creation
385+
*
386+
* @name: file name candidate
387+
* @size: length of buffer
388+
*
389+
* some x_tables modules wish to create a file in /proc.
390+
* This function makes sure that the name is suitable for this
391+
* purpose, it checks that name is NUL terminated and isn't a 'special'
392+
* name, like "..".
393+
*
394+
* returns negative number on error or 0 if name is useable.
395+
*/
396+
int xt_check_proc_name(const char *name, unsigned int size)
397+
{
398+
if (name[0] == '\0')
399+
return -EINVAL;
400+
401+
if (strnlen(name, size) == size)
402+
return -ENAMETOOLONG;
403+
404+
if (strcmp(name, ".") == 0 ||
405+
strcmp(name, "..") == 0 ||
406+
strchr(name, '/'))
407+
return -EINVAL;
408+
409+
return 0;
410+
}
411+
EXPORT_SYMBOL(xt_check_proc_name);
412+
383413
int xt_check_match(struct xt_mtchk_param *par,
384414
unsigned int size, u_int8_t proto, bool inv_proto)
385415
{

net/netfilter/xt_hashlimit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,9 @@ static int hashlimit_mt_check(const struct xt_mtchk_param *par)
668668

669669
if (info->cfg.gc_interval == 0 || info->cfg.expire == 0)
670670
return -EINVAL;
671-
if (info->name[sizeof(info->name)-1] != '\0')
672-
return -EINVAL;
671+
ret = xt_check_proc_name(info->name, sizeof(info->name));
672+
if (ret)
673+
return ret;
673674
if (par->family == NFPROTO_IPV4) {
674675
if (info->cfg.srcmask > 32 || info->cfg.dstmask > 32)
675676
return -EINVAL;

net/netfilter/xt_recent.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ static int recent_mt_check(const struct xt_mtchk_param *par,
355355
info->hit_count, ip_pkt_list_tot);
356356
return -EINVAL;
357357
}
358-
if (info->name[0] == '\0' ||
359-
strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
360-
return -EINVAL;
358+
ret = xt_check_proc_name(info->name, sizeof(info->name));
359+
if (ret)
360+
return ret;
361361

362362
mutex_lock(&recent_mutex);
363363
t = recent_table_lookup(recent_net, info->name);

0 commit comments

Comments
 (0)