Skip to content

Commit

Permalink
all: merge 'th/avoid-mt-unsafe-libc'
Browse files Browse the repository at this point in the history
  • Loading branch information
thom311 committed Aug 18, 2023
2 parents 74bffbf + e3e6fd6 commit b544a3c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 23 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ AC_CONFIG_SUBDIRS([doc])

AC_CHECK_FUNCS([strerror_l])

AC_CHECK_DECLS([getprotobyname_r, getprotobynumber_r], [], [], [[
#include <netdb.h>
]])

AC_CONFIG_FILES([
Makefile
libnl-3.0.pc
Expand Down
4 changes: 4 additions & 0 deletions include/nl-priv-dynamic-core/nl-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ struct nl_msg {

/*****************************************************************************/

int nl_getprotobyname(const char *name);

bool nl_getprotobynumber(int proto, char *out_name, size_t name_len);

extern const char *nl_strerror_l(int err);

extern int __nl_read_num_str_file(const char *path,
Expand Down
69 changes: 60 additions & 9 deletions lib/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,57 @@ struct trans_list {
struct nl_list_head list;
};

int nl_getprotobyname(const char *name)
{
const struct protoent *result;

#if HAVE_DECL_GETPROTOBYNAME_R
struct protoent result_buf;
char buf[2048];
int r;

r = getprotobyname_r(name, &result_buf, buf, sizeof(buf),
(struct protoent **)&result);
if (r != 0 || result != &result_buf)
result = NULL;
#else
result = getprotobyname(name);
#endif

if (!result)
return -1;

if (result->p_proto < 0 || result->p_proto > UINT8_MAX)
return -1;
return (uint8_t)result->p_proto;
}

bool nl_getprotobynumber(int proto, char *out_name, size_t name_len)
{
const struct protoent *result;

#if HAVE_DECL_GETPROTOBYNUMBER_R
struct protoent result_buf;
char buf[2048];
int r;

r = getprotobynumber_r(proto, &result_buf, buf, sizeof(buf),
(struct protoent **)&result);
if (r != 0 || result != &result_buf)
result = NULL;
#else
result = getprotobynumber(proto);
#endif

if (!result)
return false;

if (strlen(result->p_name) >= name_len)
return false;
strcpy(out_name, result->p_name);
return true;
}

const char *nl_strerror_l(int err)
{
const char *buf;
Expand Down Expand Up @@ -870,28 +921,28 @@ int nl_str2ether_proto(const char *name)

char *nl_ip_proto2str(int proto, char *buf, size_t len)
{
struct protoent *p = getprotobynumber(proto);

if (p) {
snprintf(buf, len, "%s", p->p_name);
if (nl_getprotobynumber(proto, buf, len))
return buf;
}

snprintf(buf, len, "0x%x", proto);
return buf;
}

int nl_str2ip_proto(const char *name)
{
struct protoent *p = getprotobyname(name);
unsigned long l;
char *end;
int p;

if (p)
return p->p_proto;
if (!name)
return -NLE_INVAL;

p = nl_getprotobyname(name);
if (p >= 0)
return p;

l = strtoul(name, &end, 0);
if (l == ULONG_MAX || *end != '\0')
if (name == end || *end != '\0' || l > (unsigned long)INT_MAX)
return -NLE_OBJ_NOTFOUND;

return (int) l;
Expand Down
5 changes: 3 additions & 2 deletions lib/xfrm/ae.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p)
char flags[128], buf[128];
time_t add_time, use_time;
struct tm *add_time_tm, *use_time_tm;
struct tm tm_buf;

nl_dump_line(p, "src %s dst %s \n", nl_addr2str(ae->saddr, src, sizeof(src)),
nl_addr2str(ae->sa_id.daddr, dst, sizeof(dst)));
Expand All @@ -354,7 +355,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (ae->lifetime_cur.add_time != 0)
{
add_time = ae->lifetime_cur.add_time;
add_time_tm = gmtime (&add_time);
add_time_tm = gmtime_r (&add_time, &tm_buf);
strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
}
else
Expand All @@ -365,7 +366,7 @@ static void xfrm_ae_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (ae->lifetime_cur.use_time != 0)
{
use_time = ae->lifetime_cur.use_time;
use_time_tm = gmtime (&use_time);
use_time_tm = gmtime_r (&use_time, &tm_buf);
strftime (buf, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
}
else
Expand Down
5 changes: 3 additions & 2 deletions lib/xfrm/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p)
char flags[128], mode[128];
time_t add_time, use_time;
struct tm *add_time_tm, *use_time_tm;
struct tm tm_buf;

nl_dump_line(p, "src %s dst %s family: %s\n", nl_addr2str(sa->saddr, src, sizeof(src)),
nl_addr2str(sa->id.daddr, dst, sizeof(dst)),
Expand Down Expand Up @@ -554,7 +555,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (sa->curlft.add_time != 0)
{
add_time = sa->curlft.add_time;
add_time_tm = gmtime (&add_time);
add_time_tm = gmtime_r (&add_time, &tm_buf);
strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
}
else
Expand All @@ -565,7 +566,7 @@ static void xfrm_sa_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (sa->curlft.use_time != 0)
{
use_time = sa->curlft.use_time;
use_time_tm = gmtime (&use_time);
use_time_tm = gmtime_r (&use_time, &tm_buf);
strftime (mode, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
}
else
Expand Down
5 changes: 3 additions & 2 deletions lib/xfrm/sp.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p)
char dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
time_t add_time, use_time;
struct tm *add_time_tm, *use_time_tm;
struct tm tm_buf;

nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src, sizeof(src));
nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst));
Expand Down Expand Up @@ -417,7 +418,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (sp->curlft.add_time != 0)
{
add_time = sp->curlft.add_time;
add_time_tm = gmtime (&add_time);
add_time_tm = gmtime_r (&add_time, &tm_buf);
strftime (dst, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", add_time_tm);
}
else
Expand All @@ -428,7 +429,7 @@ static void xfrm_sp_dump_line(struct nl_object *a, struct nl_dump_params *p)
if (sp->curlft.use_time != 0)
{
use_time = sp->curlft.use_time;
use_time_tm = gmtime (&use_time);
use_time_tm = gmtime_r (&use_time, &tm_buf);
strftime (src, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", use_time_tm);
}
else
Expand Down
9 changes: 9 additions & 0 deletions tests/check-direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ START_TEST(static_checks)
ck_assert_int_le(i, RTNL_LINK_STATS_MAX);
ck_assert_int_eq(i, rtnl_link_str2stat(s));
}

ck_assert_int_eq(nl_str2ip_proto(""), -NLE_OBJ_NOTFOUND);
ck_assert_int_eq(nl_str2ip_proto("5"), 5);
ck_assert_int_eq(nl_str2ip_proto(" 13 "), -NLE_OBJ_NOTFOUND);
ck_assert_int_eq(nl_str2ip_proto("13"), 13);
ck_assert_int_eq(nl_str2ip_proto("0x13"), 0x13);
ck_assert_int_eq(nl_str2ip_proto("0342"), 0342);
ck_assert_int_eq(nl_str2ip_proto("2147483647"), 2147483647);
ck_assert_int_eq(nl_str2ip_proto("2147483648"), -NLE_OBJ_NOTFOUND);
}
END_TEST

Expand Down
8 changes: 1 addition & 7 deletions tests/cksuite-all-ematch-tree-clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ static long long my_pow(long long x, long long y)
return ret;
}

static long int generate_random(long int max)
{
srandom(time(NULL) + id);
return (random() % max);
}

static int build_children(struct nl_list_head *parent)
{
int i, num = 0;
Expand All @@ -57,7 +51,7 @@ static int build_children(struct nl_list_head *parent)
return 0;
}

num = generate_random(MAX_CHILDREN + 1);
num = _nltst_rand_u32() % ((unsigned)(MAX_CHILDREN + 1));
for (i = 0; i < num; ++i) {
child = rtnl_ematch_alloc();
if (!child) {
Expand Down
3 changes: 2 additions & 1 deletion tests/test-cache-mngr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ static void print_timestamp(FILE *fp)
struct timeval tv;
char tshort[40];
struct tm *tm;
struct tm tm_buf;

gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
tm = localtime_r(&tv.tv_sec, &tm_buf);

strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm);
fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec);
Expand Down

0 comments on commit b544a3c

Please sign in to comment.