Skip to content

Commit

Permalink
Backport fixes from Clade 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
17451k committed Jan 28, 2021
1 parent 5bacd33 commit 622c0f2
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 160 deletions.
16 changes: 12 additions & 4 deletions clade/intercept/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ cmake_minimum_required(VERSION 3.3)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_compile_options(
-Wall
-Wextra
-Wno-unused-parameter
-O3
)

add_library(data STATIC data.c)
add_library(which STATIC which.c)
add_library(env STATIC env.c)
add_library(client STATIC client.c)
target_link_libraries(data which env client)
add_library(lock STATIC lock.c)
target_link_libraries(data which env client lock)

add_library(interceptor SHARED interceptor.c)
target_link_libraries(interceptor ${CMAKE_DL_LIBS} which data env client)
target_link_libraries(interceptor ${CMAKE_DL_LIBS} which data env client lock)

add_executable(wrapper wrapper.c)
target_link_libraries(wrapper which data env client)
target_link_libraries(wrapper which data env client lock)

set_target_properties(data which env interceptor wrapper PROPERTIES C_STANDARD 11)
set_target_properties(data which env interceptor wrapper lock PROPERTIES C_STANDARD 11)
25 changes: 16 additions & 9 deletions clade/intercept/unix/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <netinet/in.h>
#include <arpa/inet.h>

#include "env.h"

static void send_data_unix(const char *msg, char *address) {
int sockfd;

Expand All @@ -40,11 +42,14 @@ static void send_data_unix(const char *msg, char *address) {
exit(EXIT_FAILURE);
}

int ret = write(sockfd, msg, strlen(msg));
ssize_t r = write(sockfd, msg, strlen(msg));

if (r == -1) {
perror("Failed to write to the socket");
}

// We need to wait until the server finished message processing and close the socket
char buf[1024];
ssize_t r;
while ((r = read(sockfd, buf, sizeof(buf)-1)) > 0) {}
}

Expand All @@ -69,18 +74,21 @@ static void send_data_inet(const char *msg, char *host, char *port) {
exit(EXIT_FAILURE);
}

int ret = write(sockfd, msg, strlen(msg));
ssize_t r = write(sockfd, msg, strlen(msg));

if (r == -1) {
perror("Failed to write to the socket");
}

// We need to wait until the server finished message processing and close the socket
char buf[1024];
ssize_t r;
while ((r = read(sockfd, buf, sizeof(buf)-1)) > 0) {}
}

char *send_data(const char *msg) {
char* host = getenv("CLADE_INET_HOST");
char* port = getenv("CLADE_INET_PORT");
char* address = getenv("CLADE_UNIX_ADDRESS");
void send_data(const char *msg) {
char* host = getenv(CLADE_INET_HOST_ENV);
char* port = getenv(CLADE_INET_PORT_ENV);
char* address = getenv(CLADE_UNIX_ADDRESS_ENV);

// Use UNIX sockets if address is not NULL
if (address) {
Expand All @@ -95,4 +103,3 @@ char *send_data(const char *msg) {
exit(EXIT_FAILURE);
}
}

93 changes: 54 additions & 39 deletions clade/intercept/unix/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

#include <sys/file.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -24,11 +23,12 @@
#include "which.h"
#include "env.h"
#include "client.h"
#include "lock.h"

#define DELIMITER "||"

static void expand_newlines(char *dest, const char *src) {
for (int i = 0; i < strlen(src); i++) {
for (size_t i = 0; i < strlen(src); i++) {
switch(src[i]) {
case '\n':
dest += sprintf(dest, "\\n");
Expand Down Expand Up @@ -58,16 +58,16 @@ static char *expand_newlines_alloc(const char *src) {
return dest;
}

static char *prepare_data(const char *path, char const *const argv[]) {
static char *prepare_exec_data(const char *path, char const *const argv[]) {
unsigned args_len = 1, written_len = 0;

// Concatenate all command-line arguments together using "||" as delimeter.
for (const char *const *arg = argv; arg && *arg; arg++) {
// Argument might be replaced by a new large string with escaped newlines
args_len += 2 * strlen(*arg) + 1;
// Each separator will require 2 additional bytes
// Each separator will require additional bytes
if ((arg + 1) && *(arg + 1))
args_len += 2;
args_len += strlen(DELIMITER);
}

// Get current working directory
Expand All @@ -88,25 +88,22 @@ static char *prepare_data(const char *path, char const *const argv[]) {
}

// Allocate memory to store the data + cwd + which + PID (50) + delimeters.
char *data = malloc(args_len + strlen(cwd) + strlen(DELIMITER) + 500 + strlen(DELIMITER)
+ strlen(correct_path) + strlen(DELIMITER) + strlen("\n"));
char *data = malloc(args_len + strlen(cwd) + strlen(DELIMITER) * 3 + 50
+ strlen(correct_path) + strlen("\n"));

if (!data) {
fprintf(stderr, "Couldn't allocate memory\n");
exit(EXIT_FAILURE);
}

written_len += sprintf(data + written_len, "%s", cwd);
written_len += sprintf(data + written_len, DELIMITER);

char *parent_id = get_parent_id();
written_len += sprintf(data + written_len, "%s", parent_id);
written_len += sprintf(data + written_len, DELIMITER);
written_len += sprintf(data + written_len, "%s%s%s%s%s%s",
cwd, DELIMITER,
parent_id, DELIMITER,
correct_path, DELIMITER
);
free(parent_id);

written_len += sprintf(data + written_len, "%s", correct_path);
written_len += sprintf(data + written_len, DELIMITER);

for (const char *const *arg = argv; arg && *arg; arg++) {
char *exp_arg = expand_newlines_alloc(*arg);
written_len += sprintf(data + written_len, "%s", exp_arg);
Expand All @@ -121,7 +118,28 @@ static char *prepare_data(const char *path, char const *const argv[]) {
return data;
}

static void store_data(char *data, char *data_file) {
static char *prepare_open_data(const char *path, int flags) {
// Allocate memory to store the CMD_ID + existence + path + " " and "\n".
char *data = malloc(sizeof(int) * 3 + strlen(" \n") + strlen(path));

if (!data) {
fprintf(stderr, "Couldn't allocate memory\n");
exit(EXIT_FAILURE);
}

int exists = 1;
if (access(path, F_OK)) {
exists = 0;
}

int cmd_id = get_cmd_id();

sprintf(data, "%d %d %d %s\n", cmd_id, exists, flags, path);

return data;
}

static void store_data(const char *data, const char *data_file) {
FILE *f = fopen(data_file, "a");
if (!f) {
fprintf(stderr, "Couldn't open %s file\n", data_file);
Expand All @@ -132,36 +150,33 @@ static void store_data(char *data, char *data_file) {
fclose(f);
}

void intercept_call(const char *path, char const *const argv[]) {
char *data_file = getenv("CLADE_INTERCEPT");
char *id_file = getenv("CLADE_ID_FILE");
void intercept_exec_call(const char *path, char const *const argv[]) {
char *data_file = getenv_or_fail(CLADE_INTERCEPT_EXEC_ENV);

if (!data_file) {
fprintf(stderr, "Environment is not prepared: CLADE_INTERCEPT is not specified\n");
exit(EXIT_FAILURE);
}

if (!id_file) {
fprintf(stderr, "Environment is not prepared: CLADE_ID_FILE is not specified\n");
exit(EXIT_FAILURE);
}

FILE *f = fopen(id_file, "r");
if (!f) {
fprintf(stderr, "Couldn't open %s file\n", id_file);
exit(EXIT_FAILURE);
}
flock(fileno(f), LOCK_EX);
clade_lock();

// Data with intercepted command which will be stored
char *data = prepare_data(path, argv);
char *data = prepare_exec_data(path, argv);

if (getenv("CLADE_PREPROCESS"))
if (getenv(CLADE_PREPROCESS_ENV))
send_data(data);
else
store_data(data, data_file);

free(data);

fclose(f);
flock(fileno(f), LOCK_UN);
clade_unlock();
}

void intercept_open_call(const char *path, int flags) {
char *data_file = getenv_or_fail(CLADE_INTERCEPT_OPEN_ENV);

clade_lock();

// Data with intercepted command which will be stored
char *data = prepare_open_data(path, flags);
store_data(data, data_file);
free(data);

clade_unlock();
}
3 changes: 2 additions & 1 deletion clade/intercept/unix/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef DATA_H
#define DATA_H

extern void intercept_call(const char *path, char const *const argv[]);
extern void intercept_exec_call(const char *path, char const *const argv[]);
extern void intercept_open_call(const char *path, int flags);

#endif /* DATA_H */
Loading

0 comments on commit 622c0f2

Please sign in to comment.