Skip to content

Commit

Permalink
Changes needed for Sysdig agent to support aarch64 (64-bit ARM) and s…
Browse files Browse the repository at this point in the history
…390x (zLinux) architectures

- Fix Little-Endian byte-ordering assumptions in HTTP parsing and IP address
  validation code
- Adjust set of LUA APIs used, to allow interoperability with the different LUA
  versions available for the different architectures

[SMAGENT-3292] Fix unterminated comment block to re-enable RAW_BREAKPOINTS for x86
  • Loading branch information
jcpittman144 authored and mstemm committed Oct 28, 2022
1 parent 3fba858 commit 3c125ff
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
18 changes: 18 additions & 0 deletions driver/bpf/filler_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ or GPL2.txt for full copies of the license.
#include <linux/in.h>
#include <linux/fdtable.h>
#include <linux/net.h>
/* SYSDIG -- Fix Little-Endian assumptions */
#include <endian.h>

#include "../ppm_flag_helpers.h"
#include "builtins.h"
Expand Down Expand Up @@ -447,6 +449,8 @@ static __always_inline u32 bpf_compute_snaplen(struct filler_data *data,
if (lookahead_size >= 5) {
u32 buf = *(u32 *)&get_buf(0);

/* SYSDIG -- Fix Little-Endian assumptions */
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (buf == 0x20544547 || // "GET "
buf == 0x54534F50 || // "POST"
buf == 0x20545550 || // "PUT "
Expand All @@ -457,6 +461,20 @@ static __always_inline u32 bpf_compute_snaplen(struct filler_data *data,
(buf == 0x50545448 && data->buf[(data->state->tail_ctx.curoff + 4) & SCRATCH_SIZE_HALF] == '/')) { // "HTTP/"
return 2000;
}
#elif __BYTE_ORDER == __BIG_ENDIAN
if (buf == 0x47455420 || // "GET "
buf == 0x504F5354 || // "POST"
buf == 0x50555420 || // "PUT "
buf == 0x44454C45 || // "DELE"
buf == 0x54524143 || // "TRAC"
buf == 0x434F4E4E || // "CONN"
buf == 0x4F505449 || // "OPTI"
(buf == 0x48545450 && data->buf[(data->state->tail_ctx.curoff + 4) & SCRATCH_SIZE_HALF] == '/')) { // "HTTP/"
return 2000;
}
#else
#error UNDEFINED __BYTE_ORDER
#endif
}
}

Expand Down
5 changes: 3 additions & 2 deletions userspace/chisel/chisel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ limitations under the License.
#ifdef HAS_LUA_CHISELS

extern "C" {
#define LUA_COMPAT_ALL
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
Expand Down Expand Up @@ -955,7 +956,7 @@ bool sinsp_chisel::parse_view_info(lua_State *ls, OUT chisel_desc* cd)
// Initializes a lua chisel
bool sinsp_chisel::init_lua_chisel(chisel_desc &cd, string const &fpath)
{
lua_State* ls = lua_open();
lua_State* ls = luaL_newstate();
if(ls == NULL)
{
return false;
Expand Down Expand Up @@ -1205,7 +1206,7 @@ void sinsp_chisel::load(string cmdstr, bool is_file)
//
// Open the script
//
m_ls = lua_open();
m_ls = luaL_newstate();

luaL_openlibs(m_ls);

Expand Down
1 change: 1 addition & 0 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ limitations under the License.

#ifdef HAS_LUA_CHISELS
extern "C" {
#define LUA_COMPAT_ALL
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
Expand Down
23 changes: 14 additions & 9 deletions userspace/libsinsp/ifinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ sinsp_ipv4_ifinfo::sinsp_ipv4_ifinfo(uint32_t addr, uint32_t netmask, uint32_t b

void sinsp_ipv4_ifinfo::convert_to_string(char * dest, const uint32_t addr)
{
uint32_t addr_network_byte_order = htonl(addr);
sprintf(
dest,
"%d.%d.%d.%d",
(addr & 0xFF),
((addr & 0xFF00) >> 8),
((addr & 0xFF0000) >> 16),
((addr & 0xFF000000) >> 24));
((addr_network_byte_order & 0xFF000000) >> 24),
((addr_network_byte_order & 0xFF0000) >> 16),
((addr_network_byte_order & 0xFF00) >> 8),
(addr_network_byte_order & 0xFF));
}

string sinsp_ipv4_ifinfo::address() const
Expand Down Expand Up @@ -92,7 +93,7 @@ uint32_t sinsp_network_interfaces::infer_ipv4_address(uint32_t destination_addre
// otherwise take the first non loopback interface
for(it = m_ipv4_interfaces.begin(); it != m_ipv4_interfaces.end(); it++)
{
if(it->m_addr != LOOPBACK_ADDR)
if(it->m_addr != ntohl(INADDR_LOOPBACK))
{
return it->m_addr;
}
Expand Down Expand Up @@ -198,11 +199,15 @@ bool sinsp_network_interfaces::is_ipv4addr_in_subnet(uint32_t addr)
vector<sinsp_ipv4_ifinfo>::iterator it;

//
// Accept everything that comes from 192.168.0.0/16 or 10.0.0.0/8
// Accept everything that comes from private internets:
// - 10.0.0.0/8
// - 192.168.0.0/16
// - 172.16.0.0/12
//
if((addr & 0x000000ff) == 0x0000000a ||
(addr & 0x0000ffff) == 0x0000a8c0 ||
(addr & 0x00003fff) == 0x000010ac)
uint32_t addr_network_byte_order = htonl(addr);
if((addr_network_byte_order & 0xff000000) == 0x0a000000 ||
(addr_network_byte_order & 0xffff0000) == 0xc0a80000 ||
(addr_network_byte_order & 0xff3f0000) == 0xac100000)
{
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions userspace/libsinsp/ifinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.

#include "tuples.h"

#define LOOPBACK_ADDR 0x0100007f

#ifndef VISIBILITY_PRIVATE
#define VISIBILITY_PRIVATE private:
#endif
Expand Down Expand Up @@ -91,4 +89,4 @@ void sinsp_network_interfaces::clear()
{
m_ipv4_interfaces.clear();
m_ipv6_interfaces.clear();
}
}

0 comments on commit 3c125ff

Please sign in to comment.