Skip to content

Commit

Permalink
netfilter: x_tables: add and use xt_check_proc_name
Browse files Browse the repository at this point in the history
commit b1d0a5d0cba4597c0394997b2d5fced3e3841b4e 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 <[email protected]>
Reported-by: Eric Dumazet <[email protected]>
Reported-by: <[email protected]>
Signed-off-by: Florian Westphal <[email protected]>
Signed-off-by: Pablo Neira Ayuso <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
Florian Westphal authored and gregkh committed Apr 8, 2018
1 parent ccf38b3 commit c6ab7c6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/linux/netfilter/x_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ unsigned int *xt_alloc_entry_offsets(unsigned int size);
bool xt_find_jump_offset(const unsigned int *offsets,
unsigned int target, unsigned int size);

int xt_check_proc_name(const char *name, unsigned int size);

int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
bool inv_proto);
int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
Expand Down
30 changes: 30 additions & 0 deletions net/netfilter/x_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,36 @@ textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
return buf;
}

/**
* xt_check_proc_name - check that name is suitable for /proc file creation
*
* @name: file name candidate
* @size: length of buffer
*
* some x_tables modules wish to create a file in /proc.
* This function makes sure that the name is suitable for this
* purpose, it checks that name is NUL terminated and isn't a 'special'
* name, like "..".
*
* returns negative number on error or 0 if name is useable.
*/
int xt_check_proc_name(const char *name, unsigned int size)
{
if (name[0] == '\0')
return -EINVAL;

if (strnlen(name, size) == size)
return -ENAMETOOLONG;

if (strcmp(name, ".") == 0 ||
strcmp(name, "..") == 0 ||
strchr(name, '/'))
return -EINVAL;

return 0;
}
EXPORT_SYMBOL(xt_check_proc_name);

int xt_check_match(struct xt_mtchk_param *par,
unsigned int size, u_int8_t proto, bool inv_proto)
{
Expand Down
11 changes: 7 additions & 4 deletions net/netfilter/xt_hashlimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,9 @@ static int hashlimit_mt_check_v1(const struct xt_mtchk_param *par)
struct hashlimit_cfg2 cfg = {};
int ret;

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

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

Expand All @@ -809,9 +810,11 @@ static int hashlimit_mt_check_v1(const struct xt_mtchk_param *par)
static int hashlimit_mt_check(const struct xt_mtchk_param *par)
{
struct xt_hashlimit_mtinfo2 *info = par->matchinfo;
int ret;

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

return hashlimit_mt_check_common(par, &info->hinfo, &info->cfg,
info->name, 2);
Expand Down
6 changes: 3 additions & 3 deletions net/netfilter/xt_recent.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ static int recent_mt_check(const struct xt_mtchk_param *par,
info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
return -EINVAL;
}
if (info->name[0] == '\0' ||
strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
return -EINVAL;
ret = xt_check_proc_name(info->name, sizeof(info->name));
if (ret)
return ret;

if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot)
nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1;
Expand Down

0 comments on commit c6ab7c6

Please sign in to comment.