Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driver API versioning fixes #207

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions driver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,16 @@ static long ppm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ret = 0;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_API_VERSION) {
ret = PPM_API_CURRENT_VERSION;
unsigned long long __user *out = (unsigned long long __user *) arg;
ret = 0;
if(put_user(PPM_API_CURRENT_VERSION, out))
ret = -EINVAL;
goto cleanup_ioctl_nolock;
} else if (cmd == PPM_IOCTL_GET_SCHEMA_VERSION) {
ret = PPM_SCHEMA_CURRENT_VERSION;
unsigned long long __user *out = (unsigned long long __user *) arg;
ret = 0;
if(put_user(PPM_SCHEMA_CURRENT_VERSION, out))
ret = -EINVAL;
goto cleanup_ioctl_nolock;
}

Expand Down
15 changes: 9 additions & 6 deletions driver/ppm_api_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
* bits 0-23: patch version
*/

#define PPM_VERSION_PACK(val, bits, shift) ((((unsigned long long)(val)) & ((1ULL << (bits)) - 1)) << (shift))
#define PPM_VERSION_UNPACK(val, bits, shift) ((((unsigned long long)(val)) >> (shift)) & ((1ULL << (bits)) - 1))

/* extract components from an API version number */
#define PPM_API_VERSION_MAJOR(ver) ((((ver) >> 44)) & (((1 << 19) - 1)))
#define PPM_API_VERSION_MINOR(ver) (((ver) >> 24) & (((1 << 20) - 1)))
#define PPM_API_VERSION_PATCH(ver) (((ver) & ((1 << 24) - 1)))
#define PPM_API_VERSION_MAJOR(ver) PPM_VERSION_UNPACK(ver, 19, 44)
#define PPM_API_VERSION_MINOR(ver) PPM_VERSION_UNPACK(ver, 20, 24)
#define PPM_API_VERSION_PATCH(ver) PPM_VERSION_UNPACK(ver, 24, 0)

/* build an API version number from components */
#define PPM_API_VERSION(major, minor, patch) \
(((major) & (((1ULL << 19) - 1) << 44)) | \
((minor) & (((1ULL << 20) - 1) << 24)) | \
((major) & (((1ULL << 24) - 1))))
PPM_VERSION_PACK(major, 19, 44) | \
PPM_VERSION_PACK(minor, 20, 24) | \
PPM_VERSION_PACK(patch, 24, 0)

#define PPM_API_CURRENT_VERSION PPM_API_VERSION( \
PPM_API_CURRENT_VERSION_MAJOR, \
Expand Down
14 changes: 6 additions & 8 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
}

// Check the API version reported
api_version = ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_GET_API_VERSION, 0);
if ((int64_t)api_version < 0)
if (ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_GET_API_VERSION, &api_version) < 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "Kernel module does not support PPM_IOCTL_GET_API_VERSION");
close(handle->m_devs[j].m_fd);
Expand All @@ -443,7 +442,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
// Make sure all devices report the same API version
if (handle->m_api_version != 0 && handle->m_api_version != api_version)
{
snprintf(error, SCAP_LASTERR_SIZE, "API version mismatch: device %s reports API version %lu.%lu.%lu, expected %lu.%lu.%lu",
snprintf(error, SCAP_LASTERR_SIZE, "API version mismatch: device %s reports API version %llu.%llu.%llu, expected %llu.%llu.%llu",
filename,
PPM_API_VERSION_MAJOR(api_version),
PPM_API_VERSION_MINOR(api_version),
Expand All @@ -462,8 +461,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
handle->m_api_version = api_version;

// Check the schema version reported
schema_version = ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_GET_SCHEMA_VERSION, 0);
if ((int64_t)schema_version < 0)
if (ioctl(handle->m_devs[j].m_fd, PPM_IOCTL_GET_SCHEMA_VERSION, &schema_version) < 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "Kernel module does not support PPM_IOCTL_GET_SCHEMA_VERSION");
close(handle->m_devs[j].m_fd);
Expand All @@ -474,7 +472,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
// Make sure all devices report the same schema version
if (handle->m_schema_version != 0 && handle->m_schema_version != schema_version)
{
snprintf(error, SCAP_LASTERR_SIZE, "Schema version mismatch: device %s reports schema version %lu.%lu.%lu, expected %lu.%lu.%lu",
snprintf(error, SCAP_LASTERR_SIZE, "Schema version mismatch: device %s reports schema version %llu.%llu.%llu, expected %llu.%llu.%llu",
filename,
PPM_API_VERSION_MAJOR(schema_version),
PPM_API_VERSION_MINOR(schema_version),
Expand Down Expand Up @@ -557,7 +555,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,

if(!scap_is_api_compatible(handle->m_api_version, SCAP_MINIMUM_DRIVER_API_VERSION))
{
snprintf(error, SCAP_LASTERR_SIZE, "Driver supports API version %lu.%lu.%lu, but running version needs %d.%d.%d",
snprintf(error, SCAP_LASTERR_SIZE, "Driver supports API version %llu.%llu.%llu, but running version needs %d.%d.%d",
PPM_API_VERSION_MAJOR(handle->m_api_version),
PPM_API_VERSION_MINOR(handle->m_api_version),
PPM_API_VERSION_PATCH(handle->m_api_version),
Expand All @@ -571,7 +569,7 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,

if(!scap_is_api_compatible(handle->m_schema_version, SCAP_MINIMUM_DRIVER_SCHEMA_VERSION))
{
snprintf(error, SCAP_LASTERR_SIZE, "Driver supports schema version %lu.%lu.%lu, but running version needs %d.%d.%d",
snprintf(error, SCAP_LASTERR_SIZE, "Driver supports schema version %llu.%llu.%llu, but running version needs %d.%d.%d",
PPM_API_VERSION_MAJOR(handle->m_schema_version),
PPM_API_VERSION_MINOR(handle->m_schema_version),
PPM_API_VERSION_PATCH(handle->m_schema_version),
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ if(NOT MINIMAL_BUILD)
endif() # MINIMAL_BUILD

include_directories("..")
include_directories(${LIBSCAP_INCLUDE_DIR})
include_directories(${LIBSCAP_INCLUDE_DIR} ${LIBSCAP_DIR}/driver)

set(LIBSINSP_UNIT_TESTS_SOURCES
cgroup_list_counter.ut.cpp
sinsp.ut.cpp
evttype_filter.ut.cpp
token_bucket.ut.cpp
ppm_api_version.ut.cpp
)

if(NOT MINIMAL_BUILD)
Expand Down
33 changes: 33 additions & 0 deletions userspace/libsinsp/test/ppm_api_version.ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright (C) 2022 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include <gtest.h>
#include <ppm_api_version.h>

TEST(api_version, unpack)
{
uint64_t ver1_2_3 = (1ULL << 44) | (2ULL << 24) | 3;
ASSERT_EQ(ver1_2_3, PPM_API_VERSION(1, 2, 3));
}

TEST(api_version, pack)
{
uint64_t ver1_2_3 = (1ULL << 44) | (2ULL << 24) | 3;
EXPECT_EQ(1u, PPM_API_VERSION_MAJOR(ver1_2_3));
EXPECT_EQ(2u, PPM_API_VERSION_MINOR(ver1_2_3));
EXPECT_EQ(3u, PPM_API_VERSION_PATCH(ver1_2_3));
}