Skip to content

Commit

Permalink
Merge pull request #337 from kronosnet/safe-handles
Browse files Browse the repository at this point in the history
[handle] validate handle in public API
  • Loading branch information
fabbione committed Apr 14, 2021
2 parents 6d5ef32 + 18ca388 commit e3bbac3
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 164 deletions.
1 change: 1 addition & 0 deletions libknet/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sources = \
handle.c \
handle_api.c \
host.c \
lib_config.c \
links.c \
links_acl.c \
links_acl_ip.c \
Expand Down
3 changes: 1 addition & 2 deletions libknet/compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ int knet_handle_compress(knet_handle_t knet_h, struct knet_handle_compress_cfg *
int savederrno = 0;
int err = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down
9 changes: 3 additions & 6 deletions libknet/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ int knet_handle_crypto_set_config(knet_handle_t knet_h,
int savederrno = 0;
int err = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -371,8 +370,7 @@ int knet_handle_crypto_rx_clear_traffic(knet_handle_t knet_h,
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -406,8 +404,7 @@ int knet_handle_crypto_use_config(knet_handle_t knet_h,
int savederrno = 0;
int err = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down
57 changes: 15 additions & 42 deletions libknet/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,6 @@
#include "transport_common.h"
#include "logging.h"

static pthread_mutex_t handle_config_mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_rwlock_t shlib_rwlock;
static uint8_t shlib_wrlock_init = 0;

static uint32_t knet_ref = 0;

static int _init_shlib_tracker(knet_handle_t knet_h)
{
int savederrno = 0;

if (!shlib_wrlock_init) {
savederrno = pthread_rwlock_init(&shlib_rwlock, NULL);
if (savederrno) {
log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize shared lib rwlock: %s",
strerror(savederrno));
errno = savederrno;
return -1;
}
shlib_wrlock_init = 1;
}

return 0;
}

static void _fini_shlib_tracker(void)
{
if (knet_ref == 0) {
pthread_rwlock_destroy(&shlib_rwlock);
shlib_wrlock_init = 0;
}
return;
}

static int _init_locks(knet_handle_t knet_h)
{
int savederrno = 0;
Expand Down Expand Up @@ -658,7 +624,7 @@ knet_handle_t knet_handle_new(knet_node_id_t host_id,
log_info(knet_h, KNET_SUB_HANDLE, "Default onwire version: %u", knet_h->onwire_ver);

/*
* init global shlib tracker
* init global shared bits
*/
savederrno = pthread_mutex_lock(&handle_config_mutex);
if (savederrno) {
Expand All @@ -670,8 +636,16 @@ knet_handle_t knet_handle_new(knet_node_id_t host_id,
return NULL;
}

knet_ref++;
if (!handle_list_init) {
qb_list_init(&handle_list.head);
handle_list_init = 1;
}

qb_list_add(&knet_h->list, &handle_list.head);

/*
* init global shlib tracker
*/
if (_init_shlib_tracker(knet_h) < 0) {
savederrno = errno;
log_err(knet_h, KNET_SUB_HANDLE, "Unable to init handle tracker: %s",
Expand Down Expand Up @@ -757,8 +731,7 @@ int knet_handle_free(knet_handle_t knet_h)
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -793,14 +766,14 @@ int knet_handle_free(knet_handle_t knet_h)
compress_fini(knet_h, 1);
_destroy_locks(knet_h);

free(knet_h);
knet_h = NULL;

(void)pthread_mutex_lock(&handle_config_mutex);
knet_ref--;
qb_list_del(&knet_h->list);
_fini_shlib_tracker();
pthread_mutex_unlock(&handle_config_mutex);

free(knet_h);
knet_h = NULL;

errno = 0;
return 0;
}
30 changes: 10 additions & 20 deletions libknet/handle_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ int knet_handle_enable_sock_notify(knet_handle_t knet_h,
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -68,8 +67,7 @@ int knet_handle_add_datafd(knet_handle_t knet_h, int *datafd, int8_t *channel)
int i;
struct epoll_event ev;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -210,8 +208,7 @@ int knet_handle_remove_datafd(knet_handle_t knet_h, int datafd)
int i;
struct epoll_event ev;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -271,8 +268,7 @@ int knet_handle_get_datafd(knet_handle_t knet_h, const int8_t channel, int *data
{
int err = 0, savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -313,8 +309,7 @@ int knet_handle_get_channel(knet_handle_t knet_h, const int datafd, int8_t *chan
int err = 0, savederrno = 0;
int i;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -373,8 +368,7 @@ int knet_handle_enable_filter(knet_handle_t knet_h,
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -404,8 +398,7 @@ int knet_handle_setfwd(knet_handle_t knet_h, unsigned int enabled)
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -481,8 +474,7 @@ int knet_handle_get_stats(knet_handle_t knet_h, struct knet_handle_stats *stats,
{
int err = 0, savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -536,8 +528,7 @@ int knet_handle_clear_stats(knet_handle_t knet_h, int clear_option)
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -569,8 +560,7 @@ int knet_handle_enable_access_lists(knet_handle_t knet_h, unsigned int enabled)
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down
30 changes: 10 additions & 20 deletions libknet/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ int knet_host_add(knet_handle_t knet_h, knet_node_id_t host_id)
struct knet_host *host = NULL;
uint8_t link_idx;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -126,8 +125,7 @@ int knet_host_remove(knet_handle_t knet_h, knet_node_id_t host_id)
struct knet_host *host, *removed;
uint8_t link_idx;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -197,8 +195,7 @@ int knet_host_set_name(knet_handle_t knet_h, knet_node_id_t host_id, const char
int savederrno = 0, err = 0;
struct knet_host *host;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -257,8 +254,7 @@ int knet_host_get_name_by_host_id(knet_handle_t knet_h, knet_node_id_t host_id,
{
int savederrno = 0, err = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -296,8 +292,7 @@ int knet_host_get_id_by_host_name(knet_handle_t knet_h, const char *name,
int savederrno = 0, err = 0, found = 0;
struct knet_host *host;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -342,8 +337,7 @@ int knet_host_get_host_list(knet_handle_t knet_h,
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -373,8 +367,7 @@ int knet_host_set_policy(knet_handle_t knet_h, knet_node_id_t host_id,
int savederrno = 0, err = 0;
uint8_t old_policy;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -423,8 +416,7 @@ int knet_host_get_policy(knet_handle_t knet_h, knet_node_id_t host_id,
{
int savederrno = 0, err = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -463,8 +455,7 @@ int knet_host_get_status(knet_handle_t knet_h, knet_node_id_t host_id,
int savederrno = 0, err = 0;
struct knet_host *host;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down Expand Up @@ -509,8 +500,7 @@ int knet_host_enable_status_change_notify(knet_handle_t knet_h,
{
int savederrno = 0;

if (!knet_h) {
errno = EINVAL;
if (!_is_valid_handle(knet_h)) {
return -1;
}

Expand Down
15 changes: 15 additions & 0 deletions libknet/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,24 @@ struct knet_handle {
uint8_t onwire_ver);
int fini_in_progress;
uint64_t flags;
struct qb_list_head list;
};

struct handle_tracker {
struct qb_list_head head;
};

/*
* lib_config stuff shared across everything
*/
extern pthread_rwlock_t shlib_rwlock; /* global shared lib load lock */
extern pthread_mutex_t handle_config_mutex;

extern struct handle_tracker handle_list;
extern uint8_t handle_list_init;
int _is_valid_handle(knet_handle_t knet_h);
int _init_shlib_tracker(knet_handle_t knet_h);
void _fini_shlib_tracker(void);

/*
* NOTE: every single operation must be implementend
Expand Down

0 comments on commit e3bbac3

Please sign in to comment.