Skip to content

Commit

Permalink
libxdp: Use IS_ERR_OR_NULL() for NULL checks on passed-in pointers
Browse files Browse the repository at this point in the history
Most of the libxdp APIs check if the argument passed in as the main object
is NULL before dereferencing it. However, the NULL checks should really use
IS_ERR_OR_NULL() as the APIs construction those objects can return error
pointers. Otherwise we'll still crash if a user accidentally passes in such
an error pointer.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
  • Loading branch information
tohojo committed Feb 3, 2023
1 parent 05071e5 commit 76bc988
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions lib/libxdp/libxdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ xdp_program__is_attached(const struct xdp_program *xdp_prog, int ifindex)
int xdp_program__set_chain_call_enabled(struct xdp_program *prog,
unsigned int action, bool enabled)
{
if (!prog || prog->prog_fd >= 0 || action >= XDP_DISPATCHER_RETVAL)
if (IS_ERR_OR_NULL(prog) || prog->prog_fd >= 0 || action >= XDP_DISPATCHER_RETVAL)
return -EINVAL;

if (enabled)
Expand All @@ -542,23 +542,23 @@ int xdp_program__set_chain_call_enabled(struct xdp_program *prog,
bool xdp_program__chain_call_enabled(const struct xdp_program *prog,
enum xdp_action action)
{
if (!prog || action >= XDP_DISPATCHER_RETVAL)
if (IS_ERR_OR_NULL(prog) || action >= XDP_DISPATCHER_RETVAL)
return false;

return !!(prog->chain_call_actions & (1U << action));
}

unsigned int xdp_program__run_prio(const struct xdp_program *prog)
{
if (!prog)
if (IS_ERR_OR_NULL(prog))
return XDP_DEFAULT_RUN_PRIO;

return prog->run_prio;
}

int xdp_program__set_run_prio(struct xdp_program *prog, unsigned int run_prio)
{
if (!prog || prog->prog_fd >= 0)
if (IS_ERR_OR_NULL(prog) || prog->prog_fd >= 0)
return -EINVAL;

prog->run_prio = run_prio;
Expand All @@ -567,42 +567,42 @@ int xdp_program__set_run_prio(struct xdp_program *prog, unsigned int run_prio)

const char *xdp_program__name(const struct xdp_program *prog)
{
if (!prog)
if (IS_ERR_OR_NULL(prog))
return NULL;

return prog->prog_name;
}

struct bpf_object *xdp_program__bpf_obj(struct xdp_program *prog)
{
if (!prog)
if (IS_ERR_OR_NULL(prog))
return NULL;

return prog->bpf_obj;
}

const unsigned char *xdp_program__tag(const struct xdp_program *prog)
{
if (!prog)
if (IS_ERR_OR_NULL(prog))
return NULL;

return prog->prog_tag;
}

uint32_t xdp_program__id(const struct xdp_program *xdp_prog)
uint32_t xdp_program__id(const struct xdp_program *prog)
{
if (!xdp_prog)
if (IS_ERR_OR_NULL(prog))
return 0;

return xdp_prog->prog_id;
return prog->prog_id;
}

int xdp_program__fd(const struct xdp_program *xdp_prog)
int xdp_program__fd(const struct xdp_program *prog)
{
if (!xdp_prog)
if (IS_ERR_OR_NULL(prog))
return -1;

return xdp_prog->prog_fd;
return prog->prog_fd;
}

int xdp_program__print_chain_call_actions(const struct xdp_program *prog,
Expand All @@ -612,7 +612,7 @@ int xdp_program__print_chain_call_actions(const struct xdp_program *prog,
char *pos = buf;
int i, len = 0;

if (!prog || !buf || !buf_len)
if (IS_ERR_OR_NULL(prog) || !buf || !buf_len)
return -EINVAL;

for (i = 0; i <= XDP_REDIRECT; i++) {
Expand Down Expand Up @@ -1292,7 +1292,7 @@ static int cmp_xdp_programs(const void *_a, const void *_b)

int xdp_program__pin(struct xdp_program *prog, const char *pin_path)
{
if (!prog || prog->prog_fd < 0)
if (IS_ERR_OR_NULL(prog) || prog->prog_fd < 0)
return -EINVAL;

return bpf_program__pin(prog->bpf_prog, pin_path);
Expand All @@ -1302,7 +1302,7 @@ static int xdp_program__load(struct xdp_program *prog)
{
int err, prog_fd;

if (!prog)
if (IS_ERR_OR_NULL(prog))
return -EINVAL;

if (prog->prog_fd >= 0)
Expand All @@ -1328,7 +1328,7 @@ static int xdp_program__load(struct xdp_program *prog)

struct xdp_program *xdp_program__clone(struct xdp_program *prog)
{
if (!prog || prog->prog_fd < 0)
if (IS_ERR_OR_NULL(prog) || prog->prog_fd < 0)
return ERR_PTR(-EINVAL);

/* Clone a loaded program struct by creating a new object from the
Expand Down Expand Up @@ -1360,7 +1360,7 @@ static int xdp_program__attach_single(struct xdp_program *prog, int ifindex,

static int xdp_multiprog__main_fd(struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return -EINVAL;

if (!mp->main_prog)
Expand All @@ -1371,15 +1371,15 @@ static int xdp_multiprog__main_fd(struct xdp_multiprog *mp)

static __u32 xdp_multiprog__main_id(struct xdp_multiprog *mp)
{
if (!mp || !mp->main_prog)
if (IS_ERR_OR_NULL(mp) || !mp->main_prog)
return 0;

return mp->main_prog->prog_id;
}

static int xdp_multiprog__hw_fd(struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return -EINVAL;

if (!mp->hw_prog)
Expand All @@ -1390,7 +1390,7 @@ static int xdp_multiprog__hw_fd(struct xdp_multiprog *mp)

static __u32 xdp_multiprog__hw_id(struct xdp_multiprog *mp)
{
if (!mp || !mp->hw_prog)
if (IS_ERR_OR_NULL(mp) || !mp->hw_prog)
return 0;

return mp->hw_prog->prog_id;
Expand Down Expand Up @@ -1513,7 +1513,7 @@ int xdp_program__attach(struct xdp_program *prog, int ifindex,
enum xdp_attach_mode mode,
unsigned int flags)
{
if (!prog || IS_ERR(prog))
if (IS_ERR_OR_NULL(prog))
return -EINVAL;

return xdp_program__attach_multi(&prog, 1, ifindex, mode, flags);
Expand Down Expand Up @@ -1653,7 +1653,7 @@ int xdp_program__detach(struct xdp_program *prog, int ifindex,
enum xdp_attach_mode mode,
unsigned int flags)
{
if (!prog || IS_ERR(prog))
if (IS_ERR_OR_NULL(prog) || IS_ERR(prog))
return -EINVAL;

return xdp_program__detach_multi(&prog, 1, ifindex, mode, flags);
Expand Down Expand Up @@ -1694,7 +1694,7 @@ static int xdp_multiprog__load(struct xdp_multiprog *mp)
char buf[100];
int err = 0;

if (!mp || !mp->main_prog || mp->is_loaded || xdp_multiprog__is_legacy(mp))
if (IS_ERR_OR_NULL(mp) || !mp->main_prog || mp->is_loaded || xdp_multiprog__is_legacy(mp))
return -EINVAL;

pr_debug("Loading multiprog dispatcher for %d programs\n",
Expand Down Expand Up @@ -1764,7 +1764,7 @@ static int xdp_multiprog__link_pinned_progs(struct xdp_multiprog *mp)
int err, lock_fd, i;
struct stat sb = {};

if (!mp || mp->first_prog)
if (IS_ERR_OR_NULL(mp) || mp->first_prog)
return -EINVAL;

bpffs_dir = get_bpffs_dir();
Expand Down Expand Up @@ -1850,7 +1850,7 @@ static int xdp_multiprog__fill_from_fd(struct xdp_multiprog *mp,
int map_fd = -1;
int err = 0;

if (!mp)
if (IS_ERR_OR_NULL(mp))
return -EINVAL;

if (prog_fd > 0) {
Expand Down Expand Up @@ -2249,7 +2249,7 @@ static int xdp_multiprog__link_prog(struct xdp_multiprog *mp,
char *attach_func;
__s32 btf_id;

if (!mp || !prog || !mp->is_loaded ||
if (IS_ERR_OR_NULL(mp) || IS_ERR_OR_NULL(prog) || !mp->is_loaded ||
mp->num_links >= mp->config.num_progs_enabled)
return -EINVAL;

Expand Down Expand Up @@ -2551,7 +2551,7 @@ static int xdp_multiprog__pin(struct xdp_multiprog *mp)
const char *bpffs_dir;
int err = 0, lock_fd;

if (!mp || xdp_multiprog__is_legacy(mp))
if (IS_ERR_OR_NULL(mp) || xdp_multiprog__is_legacy(mp))
return -EINVAL;

bpffs_dir = get_bpffs_dir();
Expand Down Expand Up @@ -2635,7 +2635,7 @@ static int xdp_multiprog__unpin(struct xdp_multiprog *mp)
const char *bpffs_dir;
int err = 0, lock_fd;

if (!mp || xdp_multiprog__is_legacy(mp))
if (IS_ERR_OR_NULL(mp) || xdp_multiprog__is_legacy(mp))
return -EINVAL;

bpffs_dir = get_bpffs_dir();
Expand Down Expand Up @@ -2702,7 +2702,7 @@ static int xdp_multiprog__attach(struct xdp_multiprog *old_mp,
{
int err = 0, prog_fd = -1, old_fd = -1, ifindex = -1;

if (!mp && !old_mp)
if (IS_ERR_OR_NULL(mp) && !old_mp)
return -EINVAL;

if (mode == XDP_MODE_HW)
Expand Down Expand Up @@ -2748,7 +2748,7 @@ int xdp_multiprog__detach(struct xdp_multiprog *mp)
{
int err = 0;

if (!mp || !mp->is_loaded)
if (IS_ERR_OR_NULL(mp) || !mp->is_loaded)
return -EINVAL;

if (mp->hw_prog) {
Expand All @@ -2771,7 +2771,7 @@ int xdp_multiprog__detach(struct xdp_multiprog *mp)
struct xdp_program *xdp_multiprog__next_prog(const struct xdp_program *prog,
const struct xdp_multiprog *mp)
{
if (!mp || xdp_multiprog__is_legacy(mp))
if (IS_ERR_OR_NULL(mp) || xdp_multiprog__is_legacy(mp))
return NULL;

if (prog)
Expand All @@ -2782,36 +2782,39 @@ struct xdp_program *xdp_multiprog__next_prog(const struct xdp_program *prog,

struct xdp_program *xdp_multiprog__hw_prog(const struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return NULL;

return mp->hw_prog;
}

enum xdp_attach_mode xdp_multiprog__attach_mode(const struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return XDP_MODE_UNSPEC;

return mp->attach_mode;
}

struct xdp_program *xdp_multiprog__main_prog(const struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return NULL;

return mp->main_prog;
}

bool xdp_multiprog__is_legacy(const struct xdp_multiprog *mp)
{
return !!(mp && mp->is_legacy);
if (IS_ERR_OR_NULL(mp))
return false;

return mp->is_legacy;
}

int xdp_multiprog__program_count(const struct xdp_multiprog *mp)
{
if (!mp)
if (IS_ERR_OR_NULL(mp))
return -EINVAL;

return mp->num_links;
Expand Down

0 comments on commit 76bc988

Please sign in to comment.