fix: close remaining PR review gaps
CI / lint (pull_request) Failing after 31s
CI / build-and-test (pull_request) Has been skipped
CI / sanitizers (address) (pull_request) Has been skipped
CI / sanitizers (undefined) (pull_request) Has been skipped
CI / fuzz-build (pull_request) Has been skipped
CI / coverage (pull_request) Has been skipped
CI / valgrind (pull_request) Has been skipped
CI / lint (pull_request) Failing after 31s
CI / build-and-test (pull_request) Has been skipped
CI / sanitizers (address) (pull_request) Has been skipped
CI / sanitizers (undefined) (pull_request) Has been skipped
CI / fuzz-build (pull_request) Has been skipped
CI / coverage (pull_request) Has been skipped
CI / valgrind (pull_request) Has been skipped
This commit is contained in:
@@ -459,7 +459,6 @@ int main(int argc, char* argv[]) {
|
|||||||
parse_environment(&env_source, &env_dest, &save_to_disk);
|
parse_environment(&env_source, &env_dest, &save_to_disk);
|
||||||
|
|
||||||
int exit_code = 0;
|
int exit_code = 0;
|
||||||
bool config_owned_by_pipeline = false;
|
|
||||||
Config* config = config_create();
|
Config* config = config_create();
|
||||||
if (!config) {
|
if (!config) {
|
||||||
fprintf(stderr, "Error: failed to allocate config\n");
|
fprintf(stderr, "Error: failed to allocate config\n");
|
||||||
@@ -542,17 +541,13 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
/* Execute transfer */
|
/* Execute transfer */
|
||||||
if (config->use_multithreading) {
|
if (config->use_multithreading) {
|
||||||
config_owned_by_pipeline = true;
|
exit_code = send_files_multithreaded(&config);
|
||||||
exit_code = send_files_multithreaded(config);
|
|
||||||
} else {
|
} else {
|
||||||
exit_code = send_files(config);
|
exit_code = send_files(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (config) {
|
if (config) {
|
||||||
if (config->log_file)
|
|
||||||
fclose(config->log_file);
|
|
||||||
if (!config_owned_by_pipeline)
|
|
||||||
config_delete(config);
|
config_delete(config);
|
||||||
}
|
}
|
||||||
return exit_code;
|
return exit_code;
|
||||||
|
|||||||
@@ -70,6 +70,8 @@ static int send_dry_run_manifest(Config* config) {
|
|||||||
|
|
||||||
/* Send the delete manifest (list of files) to the server. Returns 0 on success, -1 on failure. */
|
/* Send the delete manifest (list of files) to the server. Returns 0 on success, -1 on failure. */
|
||||||
static int send_delete_manifest(int fd, ArrayList* manifest) {
|
static int send_delete_manifest(int fd, ArrayList* manifest) {
|
||||||
|
if (!manifest)
|
||||||
|
return -1;
|
||||||
if (!send_status(fd, STATUS_MANIFEST))
|
if (!send_status(fd, STATUS_MANIFEST))
|
||||||
return -1;
|
return -1;
|
||||||
if (!send_int(fd, manifest->size))
|
if (!send_int(fd, manifest->size))
|
||||||
@@ -111,17 +113,22 @@ static int incremental_check(Client* client, File* file, const Config* config,
|
|||||||
return 1;
|
return 1;
|
||||||
if (s == STATUS_DELTA_SIGNATURE) {
|
if (s == STATUS_DELTA_SIGNATURE) {
|
||||||
Data* sig_data = receive_data(client->file_descriptor);
|
Data* sig_data = receive_data(client->file_descriptor);
|
||||||
if (!sig_data)
|
if (!sig_data) {
|
||||||
|
send_status(client->file_descriptor, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
DeltaSignature* sig = delta_signature_deserialize(sig_data);
|
DeltaSignature* sig = delta_signature_deserialize(sig_data);
|
||||||
data_destroy(sig_data);
|
data_destroy(sig_data);
|
||||||
if (!sig)
|
if (!sig) {
|
||||||
|
send_status(client->file_descriptor, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
*out_sig = sig;
|
*out_sig = sig;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
if (s != STATUS_NEXT) {
|
if (s != STATUS_NEXT) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Unexpected server status");
|
log_message(LOG_LEVEL_ERROR, "Unexpected server status");
|
||||||
|
send_status(client->file_descriptor, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -129,8 +136,10 @@ static int incremental_check(Client* client, File* file, const Config* config,
|
|||||||
|
|
||||||
static int send_delta(Client* client, File* file, DeltaSignature* sig, Config* config) {
|
static int send_delta(Client* client, File* file, DeltaSignature* sig, Config* config) {
|
||||||
Delta* delta = delta_compute(file->data->data, file->data->size, sig, config->delta_block_size);
|
Delta* delta = delta_compute(file->data->data, file->data->size, sig, config->delta_block_size);
|
||||||
|
/* The receiver is blocked after sending the signature. Every local
|
||||||
|
fallback therefore needs the explicit NEXT response before full data. */
|
||||||
if (!delta)
|
if (!delta)
|
||||||
return 1;
|
return send_status(client->file_descriptor, STATUS_NEXT) ? 1 : -1;
|
||||||
|
|
||||||
if (!delta_is_worthwhile(delta, file->data->size)) {
|
if (!delta_is_worthwhile(delta, file->data->size)) {
|
||||||
delta_destroy(delta);
|
delta_destroy(delta);
|
||||||
@@ -142,14 +151,14 @@ static int send_delta(Client* client, File* file, DeltaSignature* sig, Config* c
|
|||||||
Data* delta_data = delta_serialize(delta);
|
Data* delta_data = delta_serialize(delta);
|
||||||
delta_destroy(delta);
|
delta_destroy(delta);
|
||||||
if (!delta_data)
|
if (!delta_data)
|
||||||
return -1;
|
return send_status(client->file_descriptor, STATUS_NEXT) ? 1 : -1;
|
||||||
|
|
||||||
Data* to_send = delta_data;
|
Data* to_send = delta_data;
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
to_send = data_compress(delta_data, config->compression_level);
|
to_send = data_compress(delta_data, config->compression_level);
|
||||||
data_destroy(delta_data);
|
data_destroy(delta_data);
|
||||||
if (!to_send)
|
if (!to_send)
|
||||||
return -1;
|
return send_status(client->file_descriptor, STATUS_NEXT) ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = send_status(client->file_descriptor, STATUS_DELTA_DATA) &&
|
bool ok = send_status(client->file_descriptor, STATUS_DELTA_DATA) &&
|
||||||
@@ -279,7 +288,8 @@ int send_chunk(Client* client, Chunk* chunk, Config* config) {
|
|||||||
if (f == NULL)
|
if (f == NULL)
|
||||||
continue;
|
continue;
|
||||||
bool stream = f->data->data == NULL && f->data->size > 0;
|
bool stream = f->data->data == NULL && f->data->size > 0;
|
||||||
bool use_sendfile = (config->use_sendfile && !config->use_compression) || stream;
|
bool use_sendfile =
|
||||||
|
(config->use_sendfile && !config->use_compression) || (stream && !config->use_compression);
|
||||||
int rc = send_single_file(client, f, config, config->use_incremental, use_sendfile);
|
int rc = send_single_file(client, f, config, config->use_incremental, use_sendfile);
|
||||||
if (rc == 1)
|
if (rc == 1)
|
||||||
continue;
|
continue;
|
||||||
@@ -478,7 +488,7 @@ static int load_files_multithreaded(void* pipeline_context) {
|
|||||||
if (!context->config->use_sendfile) {
|
if (!context->config->use_sendfile) {
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
File* f = chunk->items[i];
|
File* f = chunk->items[i];
|
||||||
if (f->data->size > STREAM_THRESHOLD)
|
if (f->data->size > STREAM_THRESHOLD && !context->config->use_compression)
|
||||||
continue;
|
continue;
|
||||||
if (!file_load_data(f)) {
|
if (!file_load_data(f)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to load file data, skipping");
|
log_message(LOG_LEVEL_ERROR, "Failed to load file data, skipping");
|
||||||
@@ -630,7 +640,7 @@ int send_files(Config* config) {
|
|||||||
if (!config->use_sendfile) {
|
if (!config->use_sendfile) {
|
||||||
for (int i = 0; i < current_chunk->element_count; i++) {
|
for (int i = 0; i < current_chunk->element_count; i++) {
|
||||||
File* f = current_chunk->items[i];
|
File* f = current_chunk->items[i];
|
||||||
if (f->data->size > STREAM_THRESHOLD)
|
if (f->data->size > STREAM_THRESHOLD && !config->use_compression)
|
||||||
continue;
|
continue;
|
||||||
if (!file_load_data(f)) {
|
if (!file_load_data(f)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to load file data");
|
log_message(LOG_LEVEL_ERROR, "Failed to load file data");
|
||||||
@@ -697,7 +707,10 @@ send_fail:
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_files_multithreaded(Config* config) {
|
int send_files_multithreaded(Config** config_ptr) {
|
||||||
|
if (!config_ptr || !*config_ptr)
|
||||||
|
return 1;
|
||||||
|
Config* config = *config_ptr;
|
||||||
if (config->dry_run)
|
if (config->dry_run)
|
||||||
return send_dry_run_manifest(config);
|
return send_dry_run_manifest(config);
|
||||||
|
|
||||||
@@ -728,8 +741,13 @@ int send_files_multithreaded(Config* config) {
|
|||||||
queue_destroy(q2);
|
queue_destroy(q2);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
*config_ptr = NULL; /* context now owns config through all remaining paths */
|
||||||
if (config->use_delete)
|
if (config->use_delete)
|
||||||
context->manifest = array_list_create(free);
|
context->manifest = array_list_create(free);
|
||||||
|
if (config->use_delete && !context->manifest) {
|
||||||
|
pipeline_context_sender_destroy(context);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
thrd_t scanner, loader, sender;
|
thrd_t scanner, loader, sender;
|
||||||
bool scanner_created = false;
|
bool scanner_created = false;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
int send_chunk(Client* client, Chunk* chunk, Config* config);
|
int send_chunk(Client* client, Chunk* chunk, Config* config);
|
||||||
int send_files(Config* config);
|
int send_files(Config* config);
|
||||||
int send_files_multithreaded(Config* config);
|
/* Takes ownership only when *config is set to NULL on return. */
|
||||||
|
int send_files_multithreaded(Config** config);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -465,8 +465,10 @@ ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata
|
|||||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
char* cur_path = path_cat(root_directory, entry->d_name);
|
char* cur_path = path_cat(root_directory, entry->d_name);
|
||||||
if (!cur_path)
|
if (!cur_path) {
|
||||||
|
ps->failed = true;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
struct stat lstats;
|
struct stat lstats;
|
||||||
if (lstat(cur_path, &lstats) != 0) {
|
if (lstat(cur_path, &lstats) != 0) {
|
||||||
free(cur_path);
|
free(cur_path);
|
||||||
|
|||||||
+54
-21
@@ -36,8 +36,10 @@ static bool tls_client_identity_allowed(SSL* ssl) {
|
|||||||
char common_name[256];
|
char common_name[256];
|
||||||
int length = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate), NID_commonName,
|
int length = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate), NID_commonName,
|
||||||
common_name, sizeof(common_name));
|
common_name, sizeof(common_name));
|
||||||
bool allowed = length >= 0 && (size_t)length < sizeof(common_name) &&
|
size_t required_length = strlen(required_client_cn);
|
||||||
strcmp(common_name, required_client_cn) == 0;
|
bool allowed = length >= 0 && (size_t)length == required_length &&
|
||||||
|
required_length < sizeof(common_name) &&
|
||||||
|
memcmp(common_name, required_client_cn, required_length) == 0;
|
||||||
X509_free(certificate);
|
X509_free(certificate);
|
||||||
return allowed;
|
return allowed;
|
||||||
}
|
}
|
||||||
@@ -54,19 +56,44 @@ static bool valid_batch_path(const char* path) {
|
|||||||
|
|
||||||
static bool __attribute__((unused)) configure_authorization(const char* root) {
|
static bool __attribute__((unused)) configure_authorization(const char* root) {
|
||||||
char resolved[PATH_MAX];
|
char resolved[PATH_MAX];
|
||||||
if (!root || !realpath(root, resolved))
|
if (!root) {
|
||||||
|
file_set_authorized_root(-1, NULL);
|
||||||
|
utils_set_authorized_root(-1, NULL);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
int root_fd = open(root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
|
if (root_fd < 0) {
|
||||||
|
file_set_authorized_root(-1, NULL);
|
||||||
|
utils_set_authorized_root(-1, NULL);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
char fd_path[64];
|
||||||
|
int fd_path_length = snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", root_fd);
|
||||||
|
if (fd_path_length < 0 || (size_t)fd_path_length >= sizeof(fd_path) ||
|
||||||
|
!realpath(fd_path, resolved)) {
|
||||||
|
close(root_fd);
|
||||||
|
file_set_authorized_root(-1, NULL);
|
||||||
|
utils_set_authorized_root(-1, NULL);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
authorized_root = str_dup(resolved);
|
authorized_root = str_dup(resolved);
|
||||||
if (!authorized_root)
|
if (!authorized_root) {
|
||||||
|
close(root_fd);
|
||||||
|
file_set_authorized_root(-1, NULL);
|
||||||
|
utils_set_authorized_root(-1, NULL);
|
||||||
return false;
|
return false;
|
||||||
authorized_root_fd = open(resolved, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
}
|
||||||
if (authorized_root_fd < 0) {
|
authorized_root_fd = root_fd;
|
||||||
|
if (!file_set_authorized_root(authorized_root_fd, authorized_root) ||
|
||||||
|
!utils_set_authorized_root(authorized_root_fd, authorized_root)) {
|
||||||
|
file_set_authorized_root(-1, NULL);
|
||||||
|
utils_set_authorized_root(-1, NULL);
|
||||||
|
close(authorized_root_fd);
|
||||||
|
authorized_root_fd = -1;
|
||||||
free(authorized_root);
|
free(authorized_root);
|
||||||
authorized_root = NULL;
|
authorized_root = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
file_set_authorized_root(authorized_root_fd, authorized_root);
|
|
||||||
utils_set_authorized_root(authorized_root_fd, authorized_root);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +165,11 @@ int receive_files(Config* config, int fd) {
|
|||||||
}
|
}
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char* full_path = path_cat(config->receive_root_directory, check_path);
|
char* full_path = path_cat(config->receive_root_directory, check_path);
|
||||||
|
if (!full_path) {
|
||||||
|
free(check_path);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
bool has_old = full_path && file_stat_secure(full_path, &st);
|
bool has_old = full_path && file_stat_secure(full_path, &st);
|
||||||
bool match = has_old && (unsigned long long)st.st_size == check_size &&
|
bool match = has_old && (unsigned long long)st.st_size == check_size &&
|
||||||
(long long)st.st_mtime == check_mtime;
|
(long long)st.st_mtime == check_mtime;
|
||||||
@@ -171,9 +203,10 @@ int receive_files(Config* config, int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (status == STATUS_MANIFEST) {
|
if (status == STATUS_MANIFEST) {
|
||||||
if (receive_manifest(fd, config, &status) != 0)
|
if (receive_manifest(fd, config, &status) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (status != STATUS_FINISHED) {
|
if (status != STATUS_FINISHED) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
|
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
|
||||||
send_status(fd, STATUS_ERROR);
|
send_status(fd, STATUS_ERROR);
|
||||||
@@ -209,24 +242,24 @@ void handler(int file_descriptor) {
|
|||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
char resolved_destination[PATH_MAX];
|
char* destination = config->receive_root_directory;
|
||||||
char* canonical_destination = realpath(config->receive_root_directory, NULL);
|
char* joined_destination = NULL;
|
||||||
const char* destination =
|
if (destination && destination[0] != '/')
|
||||||
canonical_destination ? canonical_destination : config->receive_root_directory;
|
joined_destination = path_cat(authorized_root, destination);
|
||||||
if (has_path_traversal(destination) || !path_is_within(authorized_root, destination)) {
|
if (joined_destination)
|
||||||
|
destination = joined_destination;
|
||||||
|
if (!destination || has_path_traversal(destination) ||
|
||||||
|
!path_is_within(authorized_root, destination)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Rejected destination outside authorized root");
|
log_message(LOG_LEVEL_ERROR, "Rejected destination outside authorized root");
|
||||||
free(canonical_destination);
|
free(joined_destination);
|
||||||
config_delete(config);
|
config_delete(config);
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (canonical_destination)
|
if (joined_destination) {
|
||||||
snprintf(resolved_destination, sizeof(resolved_destination), "%s", canonical_destination);
|
|
||||||
else
|
|
||||||
snprintf(resolved_destination, sizeof(resolved_destination), "%s", destination);
|
|
||||||
free(canonical_destination);
|
|
||||||
free(config->receive_root_directory);
|
free(config->receive_root_directory);
|
||||||
config->receive_root_directory = str_dup(resolved_destination);
|
config->receive_root_directory = joined_destination;
|
||||||
|
}
|
||||||
if (!config->receive_root_directory) {
|
if (!config->receive_root_directory) {
|
||||||
config_delete(config);
|
config_delete(config);
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
|
|||||||
+17
-2
@@ -13,6 +13,7 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "metadata.h"
|
#include "metadata.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
/* Maximum individual file data size within a chunk (64 MB) */
|
/* Maximum individual file data size within a chunk (64 MB) */
|
||||||
#define MAX_FILE_DATA_SIZE (64ULL * 1024 * 1024)
|
#define MAX_FILE_DATA_SIZE (64ULL * 1024 * 1024)
|
||||||
@@ -82,8 +83,14 @@ static unsigned long long per_file_serialize_size(File* file, bool use_metadata)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Data* chunk_serialize(Chunk* chunk, bool use_metadata) {
|
Data* chunk_serialize(Chunk* chunk, bool use_metadata) {
|
||||||
|
if (!chunk || chunk->element_count < 0 || (chunk->element_count > 0 && chunk->items == NULL))
|
||||||
|
return NULL;
|
||||||
unsigned long long data_size = 0;
|
unsigned long long data_size = 0;
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
|
if (!chunk->items[i] || !chunk->items[i]->path || !chunk->items[i]->data ||
|
||||||
|
(chunk->items[i]->data->size > 0 && !chunk->items[i]->data->data) ||
|
||||||
|
chunk->items[i]->path[0] == '\0' || has_path_traversal(chunk->items[i]->path))
|
||||||
|
return NULL;
|
||||||
unsigned long long file_size = per_file_serialize_size(chunk->items[i], use_metadata);
|
unsigned long long file_size = per_file_serialize_size(chunk->items[i], use_metadata);
|
||||||
if (file_size == 0 || file_size > ULLONG_MAX - data_size || data_size + file_size > SIZE_MAX)
|
if (file_size == 0 || file_size > ULLONG_MAX - data_size || data_size + file_size > SIZE_MAX)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -116,6 +123,8 @@ Data* chunk_serialize(Chunk* chunk, bool use_metadata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Chunk* chunk_deserialize(Data* data, bool use_metadata) {
|
Chunk* chunk_deserialize(Data* data, bool use_metadata) {
|
||||||
|
if (!data || (!data->data && data->size != 0))
|
||||||
|
return NULL;
|
||||||
ArrayList* files = array_list_create(file_destroy);
|
ArrayList* files = array_list_create(file_destroy);
|
||||||
if (files == NULL)
|
if (files == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -165,6 +174,12 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
|
|||||||
data_pointer += path_len;
|
data_pointer += path_len;
|
||||||
remaining_size -= path_len;
|
remaining_size -= path_len;
|
||||||
|
|
||||||
|
if (path_len == 0 || has_path_traversal(path)) {
|
||||||
|
free(path);
|
||||||
|
array_list_delete(files);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
File* file = file_create(path);
|
File* file = file_create(path);
|
||||||
free(path);
|
free(path);
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
@@ -285,14 +300,14 @@ Data* chunk_compress(Chunk* chunk, int compression_level, bool use_metadata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Chunk* receive_chunk_data(int fd, const Config* config) {
|
Chunk* receive_chunk_data(int fd, const Config* config) {
|
||||||
Data* chunk_data = receive_data(fd);
|
Data* chunk_data = receive_data_limited(fd, MAX_CHUNK_SIZE);
|
||||||
if (chunk_data == NULL) {
|
if (chunk_data == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Data* data_to_process = chunk_data;
|
Data* data_to_process = chunk_data;
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
data_to_process = data_decompress(chunk_data);
|
data_to_process = data_decompress_limited(chunk_data, MAX_CHUNK_SIZE);
|
||||||
data_destroy(chunk_data);
|
data_destroy(chunk_data);
|
||||||
if (data_to_process == NULL) {
|
if (data_to_process == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <zstd.h>
|
#include <zstd.h>
|
||||||
@@ -69,7 +71,10 @@ Data* data_compress(Data* data_to_compress, int compression_level) {
|
|||||||
return compressed_data;
|
return compressed_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* data_decompress(Data* compressed_data) {
|
Data* data_decompress_limited(Data* compressed_data, size_t maximum_size) {
|
||||||
|
if (!compressed_data || (!compressed_data->data && compressed_data->size != 0) ||
|
||||||
|
maximum_size == 0)
|
||||||
|
return NULL;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
|
log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
|
||||||
unsigned long long dst_size =
|
unsigned long long dst_size =
|
||||||
ZSTD_getFrameContentSize(compressed_data->data, compressed_data->size);
|
ZSTD_getFrameContentSize(compressed_data->data, compressed_data->size);
|
||||||
@@ -82,15 +87,16 @@ Data* data_decompress(Data* compressed_data) {
|
|||||||
// ZSTD_CONTENTSIZE_UNKNOWN (~2^64) can cause massive allocation;
|
// ZSTD_CONTENTSIZE_UNKNOWN (~2^64) can cause massive allocation;
|
||||||
// fall back to a conservative estimate (3x compressed size) when unknown.
|
// fall back to a conservative estimate (3x compressed size) when unknown.
|
||||||
if (dst_size == ZSTD_CONTENTSIZE_UNKNOWN) {
|
if (dst_size == ZSTD_CONTENTSIZE_UNKNOWN) {
|
||||||
|
if (compressed_data->size > ULLONG_MAX / 3)
|
||||||
|
return NULL;
|
||||||
dst_size = compressed_data->size * 3;
|
dst_size = compressed_data->size * 3;
|
||||||
if (dst_size < INITIAL_DECOMPRESS_BUF_SIZE)
|
if (dst_size < INITIAL_DECOMPRESS_BUF_SIZE)
|
||||||
dst_size = INITIAL_DECOMPRESS_BUF_SIZE;
|
dst_size = INITIAL_DECOMPRESS_BUF_SIZE;
|
||||||
if (dst_size > MAX_DECOMPRESSED_SIZE)
|
|
||||||
dst_size = MAX_DECOMPRESSED_SIZE;
|
|
||||||
}
|
}
|
||||||
if (dst_size > MAX_DECOMPRESSED_SIZE) {
|
unsigned long long hard_limit =
|
||||||
log_message(LOG_LEVEL_ERROR, "Declared decompressed size exceeds %llu bytes",
|
maximum_size < MAX_DECOMPRESSED_SIZE ? maximum_size : MAX_DECOMPRESSED_SIZE;
|
||||||
(unsigned long long)MAX_DECOMPRESSED_SIZE);
|
if (dst_size > hard_limit) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Declared decompressed size exceeds %llu bytes", hard_limit);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,6 +107,8 @@ Data* data_decompress(Data* compressed_data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t buf_size = (dst_size > 0) ? (size_t)dst_size : INITIAL_DECOMPRESS_BUF_SIZE;
|
size_t buf_size = (dst_size > 0) ? (size_t)dst_size : INITIAL_DECOMPRESS_BUF_SIZE;
|
||||||
|
if (buf_size > maximum_size)
|
||||||
|
buf_size = maximum_size;
|
||||||
Data* uncompressed_data = data_create_empty(buf_size);
|
Data* uncompressed_data = data_create_empty(buf_size);
|
||||||
if (!uncompressed_data) {
|
if (!uncompressed_data) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to allocate decompression buffer");
|
log_message(LOG_LEVEL_ERROR, "Failed to allocate decompression buffer");
|
||||||
@@ -121,7 +129,7 @@ Data* data_decompress(Data* compressed_data) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (ret > 0 && output.pos == output.size) {
|
if (ret > 0 && output.pos == output.size) {
|
||||||
if (buf_size >= MAX_DECOMPRESSED_SIZE) {
|
if (buf_size >= hard_limit || buf_size > SIZE_MAX / 2) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Decompressed data exceeds %llu bytes",
|
log_message(LOG_LEVEL_ERROR, "Decompressed data exceeds %llu bytes",
|
||||||
(unsigned long long)MAX_DECOMPRESSED_SIZE);
|
(unsigned long long)MAX_DECOMPRESSED_SIZE);
|
||||||
ZSTD_freeDCtx(dctx);
|
ZSTD_freeDCtx(dctx);
|
||||||
@@ -129,8 +137,8 @@ Data* data_decompress(Data* compressed_data) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
buf_size *= 2;
|
buf_size *= 2;
|
||||||
if (buf_size > MAX_DECOMPRESSED_SIZE)
|
if (buf_size > hard_limit)
|
||||||
buf_size = MAX_DECOMPRESSED_SIZE;
|
buf_size = (size_t)hard_limit;
|
||||||
void* new_data = realloc(uncompressed_data->data, buf_size);
|
void* new_data = realloc(uncompressed_data->data, buf_size);
|
||||||
if (!new_data) {
|
if (!new_data) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to grow decompression buffer");
|
log_message(LOG_LEVEL_ERROR, "Failed to grow decompression buffer");
|
||||||
@@ -150,3 +158,7 @@ Data* data_decompress(Data* compressed_data) {
|
|||||||
log_message(LOG_LEVEL_DEBUG, "Decompressed data successfully");
|
log_message(LOG_LEVEL_DEBUG, "Decompressed data successfully");
|
||||||
return uncompressed_data;
|
return uncompressed_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Data* data_decompress(Data* compressed_data) {
|
||||||
|
return data_decompress_limited(compressed_data, MAX_DECOMPRESSED_SIZE);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
Data* data_compress(Data* data_to_compress, int compression_level);
|
Data* data_compress(Data* data_to_compress, int compression_level);
|
||||||
Data* data_decompress(Data* compressed_data);
|
Data* data_decompress(Data* compressed_data);
|
||||||
|
Data* data_decompress_limited(Data* compressed_data, size_t maximum_size);
|
||||||
bool compression_should_skip(const char* path);
|
bool compression_should_skip(const char* path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+42
-99
@@ -104,6 +104,14 @@ static bool valid_wire_bool(int value) {
|
|||||||
return value == 0 || value == 1;
|
return value == 0 || value == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool receive_wire_bool(int fd, bool* value) {
|
||||||
|
int wire_value;
|
||||||
|
if (!receive_int(fd, &wire_value) || !valid_wire_bool(wire_value))
|
||||||
|
return false;
|
||||||
|
*value = wire_value != 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool validate_received_config(const Config* config) {
|
static bool validate_received_config(const Config* config) {
|
||||||
return valid_wire_bool(config->save_to_disk) && valid_wire_bool(config->use_multithreading) &&
|
return valid_wire_bool(config->save_to_disk) && valid_wire_bool(config->use_multithreading) &&
|
||||||
valid_wire_bool(config->use_chunk_serialization) &&
|
valid_wire_bool(config->use_chunk_serialization) &&
|
||||||
@@ -166,6 +174,10 @@ void config_parse_ssh_dest(Config* config) {
|
|||||||
void config_delete(Config* config) {
|
void config_delete(Config* config) {
|
||||||
if (config == NULL)
|
if (config == NULL)
|
||||||
return;
|
return;
|
||||||
|
if (config->log_file) {
|
||||||
|
fclose(config->log_file);
|
||||||
|
config->log_file = NULL;
|
||||||
|
}
|
||||||
free(config->version);
|
free(config->version);
|
||||||
free(config->send_directory);
|
free(config->send_directory);
|
||||||
free(config->receive_root_directory);
|
free(config->receive_root_directory);
|
||||||
@@ -347,135 +359,66 @@ Config* config_receive(int file_descriptor) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int tmp;
|
int tmp;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->save_to_disk) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_multithreading) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_chunk_serialization) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_compression) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_metadata))
|
||||||
goto error;
|
goto error;
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->save_to_disk = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_multithreading = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_chunk_serialization = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_compression = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_metadata = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_int(file_descriptor, &tmp))
|
||||||
goto error;
|
goto error;
|
||||||
config->compression_level = tmp;
|
config->compression_level = tmp;
|
||||||
if (!receive_n_data(file_descriptor, &config->chunk_size, sizeof(config->chunk_size)))
|
if (!receive_n_data(file_descriptor, &config->chunk_size, sizeof(config->chunk_size)))
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->use_sendfile) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_delete) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_incremental) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->use_delta))
|
||||||
goto error;
|
goto error;
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_sendfile = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_delete = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_incremental = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
if (!valid_wire_bool(tmp))
|
|
||||||
goto error;
|
|
||||||
config->use_delta = tmp;
|
|
||||||
if (!receive_n_data(file_descriptor, &config->delta_block_size, sizeof(config->delta_block_size)))
|
if (!receive_n_data(file_descriptor, &config->delta_block_size, sizeof(config->delta_block_size)))
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_n_data(file_descriptor, &config->delta_max_file_size, sizeof(unsigned long long)))
|
if (!receive_n_data(file_descriptor, &config->delta_max_file_size, sizeof(unsigned long long)))
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->backup))
|
||||||
goto error;
|
goto error;
|
||||||
config->backup = tmp;
|
|
||||||
config->backup_dir = receive_str(file_descriptor);
|
config->backup_dir = receive_str(file_descriptor);
|
||||||
if (config->backup_dir == NULL)
|
if (config->backup_dir == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->follow_symlinks) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->copy_links) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->safe_links) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->copy_unsafe_links) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->preserve_hard_links) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->preserve_acls) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->preserve_xattrs) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->preserve_devices) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->preserve_sparse) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->update) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->inplace) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->append) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->append_verify) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->delete_excluded) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->delete_after))
|
||||||
goto error;
|
goto error;
|
||||||
config->follow_symlinks = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->copy_links = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->safe_links = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->copy_unsafe_links = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->preserve_hard_links = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->preserve_acls = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->preserve_xattrs = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->preserve_devices = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->preserve_sparse = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->update = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->inplace = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->append = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->append_verify = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->delete_excluded = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->delete_after = tmp;
|
|
||||||
if (!receive_n_data(file_descriptor, &config->max_delete, sizeof(config->max_delete)))
|
if (!receive_n_data(file_descriptor, &config->max_delete, sizeof(config->max_delete)))
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->relative) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->prune_empty_dirs))
|
||||||
goto error;
|
goto error;
|
||||||
config->relative = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->prune_empty_dirs = tmp;
|
|
||||||
config->temp_dir = receive_str(file_descriptor);
|
config->temp_dir = receive_str(file_descriptor);
|
||||||
if (config->temp_dir == NULL)
|
if (config->temp_dir == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->partial))
|
||||||
goto error;
|
goto error;
|
||||||
config->partial = tmp;
|
|
||||||
config->partial_dir = receive_str(file_descriptor);
|
config->partial_dir = receive_str(file_descriptor);
|
||||||
if (config->partial_dir == NULL)
|
if (config->partial_dir == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
config->suffix = receive_str(file_descriptor);
|
config->suffix = receive_str(file_descriptor);
|
||||||
if (config->suffix == NULL)
|
if (config->suffix == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_wire_bool(file_descriptor, &config->delete_before) ||
|
||||||
|
!receive_wire_bool(file_descriptor, &config->checksum))
|
||||||
goto error;
|
goto error;
|
||||||
config->delete_before = tmp;
|
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
|
||||||
goto error;
|
|
||||||
config->checksum = tmp;
|
|
||||||
config->compress_choice = receive_str(file_descriptor);
|
config->compress_choice = receive_str(file_descriptor);
|
||||||
if (config->compress_choice == NULL)
|
if (config->compress_choice == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ Data* data_create_reserve(size_t size) {
|
|||||||
}
|
}
|
||||||
d->data = NULL;
|
d->data = NULL;
|
||||||
d->size = size;
|
d->size = size;
|
||||||
|
d->protocol_charge = 0;
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,12 +34,15 @@ Data* data_create(void* data, size_t data_size) {
|
|||||||
}
|
}
|
||||||
new_data->data = data;
|
new_data->data = data;
|
||||||
new_data->size = data_size;
|
new_data->size = data_size;
|
||||||
|
new_data->protocol_charge = 0;
|
||||||
return new_data;
|
return new_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void data_destroy(Data* data) {
|
void data_destroy(Data* data) {
|
||||||
if (data == NULL)
|
if (data == NULL)
|
||||||
return;
|
return;
|
||||||
|
if (data->protocol_charge != 0)
|
||||||
|
protocol_release_memory(data->protocol_charge);
|
||||||
free(data->data);
|
free(data->data);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,14 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
void* data;
|
void* data;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
/* Non-zero only for a buffer charged to the protocol connection budget. */
|
||||||
|
size_t protocol_charge;
|
||||||
} Data;
|
} Data;
|
||||||
|
|
||||||
Data* data_create_empty(size_t data_size);
|
Data* data_create_empty(size_t data_size);
|
||||||
Data* data_create_reserve(size_t size);
|
Data* data_create_reserve(size_t size);
|
||||||
Data* data_create(void* data, size_t data_size);
|
Data* data_create(void* data, size_t data_size);
|
||||||
void data_destroy(Data* data);
|
void data_destroy(Data* data);
|
||||||
|
void protocol_release_memory(size_t charge);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+74
-56
@@ -1,6 +1,7 @@
|
|||||||
#include "delta.h"
|
#include "delta.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -36,7 +37,8 @@ DeltaSignature* delta_signature_create(const void* old_file_data, uint64_t old_f
|
|||||||
if (old_file_data == NULL || old_file_size == 0 || block_size == 0)
|
if (old_file_data == NULL || old_file_size == 0 || block_size == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (old_file_size > DELTA_MAX_FILE_SIZE || old_file_size > UINT32_MAX * (uint64_t)block_size)
|
if (old_file_size > DELTA_MAX_FILE_SIZE || block_size > DELTA_BLOCK_SIZE_MAX ||
|
||||||
|
old_file_size > UINT32_MAX * (uint64_t)block_size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
uint32_t block_count = (uint32_t)((old_file_size + block_size - 1) / block_size);
|
uint32_t block_count = (uint32_t)((old_file_size + block_size - 1) / block_size);
|
||||||
@@ -48,7 +50,11 @@ DeltaSignature* delta_signature_create(const void* old_file_data, uint64_t old_f
|
|||||||
sig->file_size = old_file_size;
|
sig->file_size = old_file_size;
|
||||||
sig->block_size = block_size;
|
sig->block_size = block_size;
|
||||||
sig->block_count = block_count;
|
sig->block_count = block_count;
|
||||||
sig->blocks = malloc(block_count * sizeof(DeltaBlockSig));
|
if (block_count == 0) {
|
||||||
|
free(sig);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
sig->blocks = malloc((size_t)block_count * sizeof(DeltaBlockSig));
|
||||||
if (!sig->blocks) {
|
if (!sig->blocks) {
|
||||||
free(sig);
|
free(sig);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -70,8 +76,11 @@ Data* delta_signature_serialize(const DeltaSignature* sig) {
|
|||||||
if (!sig)
|
if (!sig)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
uint64_t total = sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint32_t) +
|
uint64_t block_bytes = (uint64_t)sig->block_count * (sizeof(uint32_t) + sizeof(uint32_t));
|
||||||
(uint64_t)sig->block_count * (sizeof(uint32_t) + sizeof(uint32_t));
|
uint64_t total = sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint32_t) + block_bytes;
|
||||||
|
if (block_bytes > UINT64_MAX - (sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
|
||||||
|
total > SIZE_MAX)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
uint8_t* buf = malloc((size_t)total);
|
uint8_t* buf = malloc((size_t)total);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
@@ -166,8 +175,10 @@ void delta_signature_destroy(DeltaSignature* sig) {
|
|||||||
static bool ensure_capacity(DeltaInstruction** instrs, uint32_t* capacity, uint32_t count) {
|
static bool ensure_capacity(DeltaInstruction** instrs, uint32_t* capacity, uint32_t count) {
|
||||||
if (count < *capacity)
|
if (count < *capacity)
|
||||||
return true;
|
return true;
|
||||||
|
if (*capacity > MAX_DELTA_INSTRUCTIONS / 2)
|
||||||
|
return false;
|
||||||
uint32_t new_cap = *capacity * 2;
|
uint32_t new_cap = *capacity * 2;
|
||||||
DeltaInstruction* tmp = realloc(*instrs, new_cap * sizeof(DeltaInstruction));
|
DeltaInstruction* tmp = realloc(*instrs, (size_t)new_cap * sizeof(DeltaInstruction));
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
return false;
|
return false;
|
||||||
*instrs = tmp;
|
*instrs = tmp;
|
||||||
@@ -179,6 +190,8 @@ static bool flush_literal(DeltaInstruction** instrs, uint32_t* capacity, uint32_
|
|||||||
const uint8_t* data, uint64_t start, uint64_t end) {
|
const uint8_t* data, uint64_t start, uint64_t end) {
|
||||||
if (start >= end)
|
if (start >= end)
|
||||||
return true;
|
return true;
|
||||||
|
if (end - start > UINT32_MAX || *count >= MAX_DELTA_INSTRUCTIONS)
|
||||||
|
return false;
|
||||||
uint32_t lit_len = (uint32_t)(end - start);
|
uint32_t lit_len = (uint32_t)(end - start);
|
||||||
if (!ensure_capacity(instrs, capacity, *count))
|
if (!ensure_capacity(instrs, capacity, *count))
|
||||||
return false;
|
return false;
|
||||||
@@ -193,16 +206,26 @@ static bool flush_literal(DeltaInstruction** instrs, uint32_t* capacity, uint32_
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void free_instructions(DeltaInstruction* instrs, uint32_t count) {
|
||||||
|
if (!instrs)
|
||||||
|
return;
|
||||||
|
for (uint32_t i = 0; i < count; i++)
|
||||||
|
if (instrs[i].type == DELTA_INSTR_LITERAL)
|
||||||
|
free(instrs[i].literal.data);
|
||||||
|
free(instrs);
|
||||||
|
}
|
||||||
|
|
||||||
Delta* delta_compute(const void* new_file_data, uint64_t new_file_size, const DeltaSignature* sig,
|
Delta* delta_compute(const void* new_file_data, uint64_t new_file_size, const DeltaSignature* sig,
|
||||||
uint32_t block_size) {
|
uint32_t block_size) {
|
||||||
if (!new_file_data || !sig || new_file_size == 0 || block_size == 0)
|
if (!new_file_data || !sig || !sig->blocks || new_file_size == 0 || block_size == 0 ||
|
||||||
|
block_size > DELTA_BLOCK_SIZE_MAX || sig->block_size != block_size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
const uint8_t* new_data = (const uint8_t*)new_file_data;
|
const uint8_t* new_data = (const uint8_t*)new_file_data;
|
||||||
|
|
||||||
uint32_t capacity = 64;
|
uint32_t capacity = 64;
|
||||||
uint32_t count = 0;
|
uint32_t count = 0;
|
||||||
DeltaInstruction* instrs = malloc(capacity * sizeof(DeltaInstruction));
|
DeltaInstruction* instrs = malloc((size_t)capacity * sizeof(DeltaInstruction));
|
||||||
if (!instrs)
|
if (!instrs)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -246,14 +269,14 @@ Delta* delta_compute(const void* new_file_data, uint64_t new_file_size, const De
|
|||||||
if (xxh == sig->blocks[j].xxhash) {
|
if (xxh == sig->blocks[j].xxhash) {
|
||||||
if (has_literal) {
|
if (has_literal) {
|
||||||
if (!flush_literal(&instrs, &capacity, &count, new_data, literal_start, i)) {
|
if (!flush_literal(&instrs, &capacity, &count, new_data, literal_start, i)) {
|
||||||
free(instrs);
|
free_instructions(instrs, count);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
has_literal = false;
|
has_literal = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ensure_capacity(&instrs, &capacity, count)) {
|
if (!ensure_capacity(&instrs, &capacity, count)) {
|
||||||
free(instrs);
|
free_instructions(instrs, count);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
instrs[count].type = DELTA_INSTR_BLOCK_MATCH;
|
instrs[count].type = DELTA_INSTR_BLOCK_MATCH;
|
||||||
@@ -281,18 +304,14 @@ Delta* delta_compute(const void* new_file_data, uint64_t new_file_size, const De
|
|||||||
|
|
||||||
if (has_literal) {
|
if (has_literal) {
|
||||||
if (!flush_literal(&instrs, &capacity, &count, new_data, literal_start, new_file_size)) {
|
if (!flush_literal(&instrs, &capacity, &count, new_data, literal_start, new_file_size)) {
|
||||||
free(instrs);
|
free_instructions(instrs, count);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Delta* delta = malloc(sizeof(Delta));
|
Delta* delta = malloc(sizeof(Delta));
|
||||||
if (!delta) {
|
if (!delta) {
|
||||||
for (uint32_t k = 0; k < count; k++) {
|
free_instructions(instrs, count);
|
||||||
if (instrs[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(instrs[k].literal.data);
|
|
||||||
}
|
|
||||||
free(instrs);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,11 +321,24 @@ Delta* delta_compute(const void* new_file_data, uint64_t new_file_size, const De
|
|||||||
delta->delta_size = 0;
|
delta->delta_size = 0;
|
||||||
|
|
||||||
for (uint32_t k = 0; k < count; k++) {
|
for (uint32_t k = 0; k < count; k++) {
|
||||||
|
if (delta->delta_size == UINT64_MAX) {
|
||||||
|
delta_destroy(delta);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
delta->delta_size += 1;
|
delta->delta_size += 1;
|
||||||
if (instrs[k].type == DELTA_INSTR_BLOCK_MATCH) {
|
if (instrs[k].type == DELTA_INSTR_BLOCK_MATCH) {
|
||||||
|
if (delta->delta_size > UINT64_MAX - sizeof(uint32_t) * 3) {
|
||||||
|
delta_destroy(delta);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
delta->delta_size += sizeof(uint32_t) * 3;
|
delta->delta_size += sizeof(uint32_t) * 3;
|
||||||
} else {
|
} else {
|
||||||
delta->delta_size += sizeof(uint32_t) + instrs[k].literal.length;
|
uint64_t extra = sizeof(uint32_t) + instrs[k].literal.length;
|
||||||
|
if (delta->delta_size > UINT64_MAX - extra) {
|
||||||
|
delta_destroy(delta);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
delta->delta_size += extra;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +349,12 @@ Data* delta_serialize(const Delta* delta) {
|
|||||||
if (!delta)
|
if (!delta)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
uint64_t total = sizeof(uint64_t) + sizeof(uint32_t) + delta->delta_size;
|
if (delta->instruction_count > 0 && !delta->instructions)
|
||||||
|
return NULL;
|
||||||
|
uint64_t header_size = sizeof(uint64_t) + sizeof(uint32_t);
|
||||||
|
if (delta->delta_size > UINT64_MAX - header_size || header_size + delta->delta_size > SIZE_MAX)
|
||||||
|
return NULL;
|
||||||
|
uint64_t total = header_size + delta->delta_size;
|
||||||
uint8_t* buf = malloc((size_t)total);
|
uint8_t* buf = malloc((size_t)total);
|
||||||
if (!buf)
|
if (!buf)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -375,8 +412,10 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
delta->instructions = malloc(delta->instruction_count * sizeof(DeltaInstruction));
|
delta->instructions = delta->instruction_count == 0
|
||||||
if (!delta->instructions) {
|
? NULL
|
||||||
|
: malloc((size_t)delta->instruction_count * sizeof(DeltaInstruction));
|
||||||
|
if (delta->instruction_count > 0 && !delta->instructions) {
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -385,11 +424,7 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
|
|
||||||
for (uint32_t i = 0; i < delta->instruction_count; i++) {
|
for (uint32_t i = 0; i < delta->instruction_count; i++) {
|
||||||
if (pos >= data->size) {
|
if (pos >= data->size) {
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -401,12 +436,8 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
delta->delta_size += 1;
|
delta->delta_size += 1;
|
||||||
|
|
||||||
if (type == DELTA_OP_BLOCK_MATCH) {
|
if (type == DELTA_OP_BLOCK_MATCH) {
|
||||||
if (pos + sizeof(uint32_t) * 3 > data->size) {
|
if (data->size - pos < sizeof(uint32_t) * 3) {
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -419,12 +450,8 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
pos += sizeof(uint32_t);
|
pos += sizeof(uint32_t);
|
||||||
delta->delta_size += sizeof(uint32_t) * 3;
|
delta->delta_size += sizeof(uint32_t) * 3;
|
||||||
} else if (type == DELTA_OP_LITERAL) {
|
} else if (type == DELTA_OP_LITERAL) {
|
||||||
if (pos + sizeof(uint32_t) > data->size) {
|
if (data->size - pos < sizeof(uint32_t)) {
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -433,23 +460,15 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
pos += sizeof(uint32_t);
|
pos += sizeof(uint32_t);
|
||||||
|
|
||||||
uint32_t lit_len = delta->instructions[i].literal.length;
|
uint32_t lit_len = delta->instructions[i].literal.length;
|
||||||
if (pos + lit_len > data->size) {
|
if (lit_len > data->size - pos) {
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
delta->instructions[i].literal.data = malloc(lit_len);
|
delta->instructions[i].literal.data = malloc(lit_len ? lit_len : 1);
|
||||||
if (!delta->instructions[i].literal.data) {
|
if (!delta->instructions[i].literal.data) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to allocate %u bytes for literal data", lit_len);
|
log_message(LOG_LEVEL_ERROR, "Failed to allocate %u bytes for literal data", lit_len);
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -457,11 +476,7 @@ Delta* delta_deserialize(const Data* data) {
|
|||||||
pos += lit_len;
|
pos += lit_len;
|
||||||
delta->delta_size += sizeof(uint32_t) + lit_len;
|
delta->delta_size += sizeof(uint32_t) + lit_len;
|
||||||
} else {
|
} else {
|
||||||
for (uint32_t k = 0; k < i; k++) {
|
free_instructions(delta->instructions, i);
|
||||||
if (delta->instructions[k].type == DELTA_INSTR_LITERAL)
|
|
||||||
free(delta->instructions[k].literal.data);
|
|
||||||
}
|
|
||||||
free(delta->instructions);
|
|
||||||
free(delta);
|
free(delta);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -477,7 +492,7 @@ void* delta_apply(const void* old_data, uint64_t old_size, const Delta* delta,
|
|||||||
delta->new_file_size > DELTA_MAX_FILE_SIZE || delta->new_file_size > SIZE_MAX)
|
delta->new_file_size > DELTA_MAX_FILE_SIZE || delta->new_file_size > SIZE_MAX)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
void* output = malloc((size_t)delta->new_file_size);
|
void* output = malloc(delta->new_file_size ? (size_t)delta->new_file_size : 1);
|
||||||
if (!output)
|
if (!output)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -502,7 +517,7 @@ void* delta_apply(const void* old_data, uint64_t old_size, const Delta* delta,
|
|||||||
}
|
}
|
||||||
memcpy(out + out_pos, old + src_offset, len);
|
memcpy(out + out_pos, old + src_offset, len);
|
||||||
out_pos += len;
|
out_pos += len;
|
||||||
} else {
|
} else if (delta->instructions[i].type == DELTA_INSTR_LITERAL) {
|
||||||
uint32_t len = delta->instructions[i].literal.length;
|
uint32_t len = delta->instructions[i].literal.length;
|
||||||
if (out_pos > delta->new_file_size || (uint64_t)len > delta->new_file_size - out_pos) {
|
if (out_pos > delta->new_file_size || (uint64_t)len > delta->new_file_size - out_pos) {
|
||||||
free(output);
|
free(output);
|
||||||
@@ -510,6 +525,9 @@ void* delta_apply(const void* old_data, uint64_t old_size, const Delta* delta,
|
|||||||
}
|
}
|
||||||
memcpy(out + out_pos, delta->instructions[i].literal.data, len);
|
memcpy(out + out_pos, delta->instructions[i].literal.data, len);
|
||||||
out_pos += len;
|
out_pos += len;
|
||||||
|
} else {
|
||||||
|
free(output);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,7 +563,7 @@ bool delta_should_attempt(uint64_t old_size, uint64_t new_size, uint64_t max_fil
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool delta_is_worthwhile(const Delta* delta, uint64_t new_file_size) {
|
bool delta_is_worthwhile(const Delta* delta, uint64_t new_file_size) {
|
||||||
if (!delta || delta->instruction_count == 0)
|
if (!delta || delta->instruction_count == 0 || new_file_size == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool has_match = false;
|
bool has_match = false;
|
||||||
|
|||||||
+148
-236
@@ -24,7 +24,7 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
#define MAX_SERVER_DELETE_COUNT 100000U
|
#define MAX_SERVER_DELETE_COUNT 100000U
|
||||||
#define MAX_FILE_DATA_SIZE (64ULL * 1024 * 1024)
|
#define MAX_FILE_DATA_SIZE MAX_RECEIVE_FILE_SIZE
|
||||||
|
|
||||||
bool file_checksum(File* file, uint64_t* checksum) {
|
bool file_checksum(File* file, uint64_t* checksum) {
|
||||||
if (!file || !checksum || !file->data)
|
if (!file || !checksum || !file->data)
|
||||||
@@ -48,7 +48,7 @@ File* file_create(const char* path) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int path_len = strlen(path);
|
size_t path_len = strlen(path);
|
||||||
file->path = (char*)malloc(path_len + 1);
|
file->path = (char*)malloc(path_len + 1);
|
||||||
if (file->path == NULL) {
|
if (file->path == NULL) {
|
||||||
free(file);
|
free(file);
|
||||||
@@ -104,7 +104,7 @@ void file_metadata_destroy(void* metadata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool file_load_data(File* file) {
|
bool file_load_data(File* file) {
|
||||||
if (file == NULL)
|
if (file == NULL || !file->data)
|
||||||
return false;
|
return false;
|
||||||
if (file->data->data == NULL) {
|
if (file->data->data == NULL) {
|
||||||
if (file->data->size == 0)
|
if (file->data->size == 0)
|
||||||
@@ -128,7 +128,7 @@ bool file_load_data(File* file) {
|
|||||||
|
|
||||||
bool file_send_single_calls(File* file, int file_descriptor, bool use_metadata,
|
bool file_send_single_calls(File* file, int file_descriptor, bool use_metadata,
|
||||||
int compression_level, bool send_path) {
|
int compression_level, bool send_path) {
|
||||||
if (!file || !file->path || !file->data)
|
if (!file || !file->path || !file->data || (file->data->size != 0 && !file->data->data))
|
||||||
return false;
|
return false;
|
||||||
const Data* data_to_send = file->data;
|
const Data* data_to_send = file->data;
|
||||||
Data* compressed_data = NULL;
|
Data* compressed_data = NULL;
|
||||||
@@ -159,6 +159,7 @@ bool file_send_single_calls(File* file, int file_descriptor, bool use_metadata,
|
|||||||
static bool to_disk_secure(const char* path, const void* data, unsigned long long data_size,
|
static bool to_disk_secure(const char* path, const void* data, unsigned long long data_size,
|
||||||
bool inplace, bool sparse, const FileMetadata* metadata);
|
bool inplace, bool sparse, const FileMetadata* metadata);
|
||||||
static int open_secure_parent(const char* path, char** leaf_out, bool create_dirs);
|
static int open_secure_parent(const char* path, char** leaf_out, bool create_dirs);
|
||||||
|
static bool ensure_directory_secure(const char* path);
|
||||||
|
|
||||||
bool file_path_exists_secure(const char* path) {
|
bool file_path_exists_secure(const char* path) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@@ -184,15 +185,23 @@ static bool rename_secure(const char* old_path, const char* new_path);
|
|||||||
static int authorized_root_fd = -1;
|
static int authorized_root_fd = -1;
|
||||||
static char* authorized_root_path;
|
static char* authorized_root_path;
|
||||||
|
|
||||||
void file_set_authorized_root(int fd, const char* canonical_path) {
|
static bool path_is_within_root(const char* root, const char* path) {
|
||||||
authorized_root_fd = fd;
|
size_t root_len = strlen(root);
|
||||||
free(authorized_root_path);
|
return strncmp(root, path, root_len) == 0 && (path[root_len] == '\0' || path[root_len] == '/');
|
||||||
authorized_root_path = canonical_path ? str_dup(canonical_path) : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool path_is_within_root(const char* root, const char* path) {
|
bool file_set_authorized_root(int fd, const char* canonical_path) {
|
||||||
size_t n = strlen(root);
|
char* path_copy = canonical_path ? str_dup(canonical_path) : NULL;
|
||||||
return strncmp(root, path, n) == 0 && (path[n] == '\0' || path[n] == '/');
|
if (canonical_path && !path_copy) {
|
||||||
|
authorized_root_fd = -1;
|
||||||
|
free(authorized_root_path);
|
||||||
|
authorized_root_path = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
authorized_root_fd = fd;
|
||||||
|
free(authorized_root_path);
|
||||||
|
authorized_root_path = path_copy;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_save_to_disk(const char* root_directory, const File* file, const Config* config) {
|
bool file_save_to_disk(const char* root_directory, const File* file, const Config* config) {
|
||||||
@@ -202,9 +211,11 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
|||||||
const char* backup_suffix = (config && config->suffix) ? config->suffix : "~";
|
const char* backup_suffix = (config && config->suffix) ? config->suffix : "~";
|
||||||
const char* backup_dir = (config && config->backup_dir) ? config->backup_dir : NULL;
|
const char* backup_dir = (config && config->backup_dir) ? config->backup_dir : NULL;
|
||||||
const char* partial_dir = (config && config->partial_dir) ? config->partial_dir : NULL;
|
const char* partial_dir = (config && config->partial_dir) ? config->partial_dir : NULL;
|
||||||
char *confined_backup = NULL, *confined_partial = NULL;
|
char *confined_backup = NULL, *confined_partial = NULL, *disk_path = NULL;
|
||||||
|
char *backup_path = NULL, *parent_copy = NULL;
|
||||||
|
|
||||||
if (!file || !file->path || !file->data || has_path_traversal(file->path) ||
|
if (!file || !file->path || !file->data || (file->data->size != 0 && !file->data->data) ||
|
||||||
|
has_path_traversal(file->path) ||
|
||||||
(backup_enabled &&
|
(backup_enabled &&
|
||||||
(!backup_suffix || backup_suffix[0] == '\0' || strchr(backup_suffix, '/') != NULL ||
|
(!backup_suffix || backup_suffix[0] == '\0' || strchr(backup_suffix, '/') != NULL ||
|
||||||
strcmp(backup_suffix, ".") == 0 || strcmp(backup_suffix, "..") == 0))) {
|
strcmp(backup_suffix, ".") == 0 || strcmp(backup_suffix, "..") == 0))) {
|
||||||
@@ -224,45 +235,20 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* resolved_root = NULL;
|
|
||||||
const char* actual_root =
|
const char* actual_root =
|
||||||
(partial_dir && config && config->partial) ? confined_partial : root_directory;
|
(partial_dir && config && config->partial) ? confined_partial : root_directory;
|
||||||
resolved_root = realpath(actual_root, NULL);
|
disk_path = path_cat(actual_root, file->path);
|
||||||
if (resolved_root == NULL) {
|
|
||||||
if (mkdir_r(actual_root)) {
|
|
||||||
resolved_root = realpath(actual_root, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (resolved_root == NULL) {
|
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to resolve destination root: %s", actual_root);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
char* resolved_base = realpath(root_directory, NULL);
|
|
||||||
if (resolved_base == NULL || !path_is_within_root(resolved_base, resolved_root)) {
|
|
||||||
free(resolved_base);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(resolved_root);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
free(resolved_base);
|
|
||||||
|
|
||||||
char* disk_path = path_cat(resolved_root, file->path);
|
|
||||||
if (disk_path == NULL) {
|
if (disk_path == NULL) {
|
||||||
free(confined_backup);
|
free(confined_backup);
|
||||||
free(confined_partial);
|
free(confined_partial);
|
||||||
free(resolved_root);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --update is receiver-side policy: never replace a newer destination. */
|
/* --update is receiver-side policy: never replace a newer destination. */
|
||||||
if (config && config->update) {
|
if (config && config->update) {
|
||||||
struct stat destination_stat;
|
struct stat destination_stat;
|
||||||
if (stat(disk_path, &destination_stat) == 0 && file->metadata &&
|
if (file_stat_secure(disk_path, &destination_stat) && file->metadata &&
|
||||||
destination_stat.st_mtime > file->metadata->mtime_sec) {
|
destination_stat.st_mtime > file->metadata->mtime_sec) {
|
||||||
free(resolved_root);
|
|
||||||
free(confined_backup);
|
free(confined_backup);
|
||||||
free(confined_partial);
|
free(confined_partial);
|
||||||
free(disk_path);
|
free(disk_path);
|
||||||
@@ -272,99 +258,50 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
|||||||
|
|
||||||
if (backup_enabled) {
|
if (backup_enabled) {
|
||||||
struct stat backup_stat;
|
struct stat backup_stat;
|
||||||
if (stat(disk_path, &backup_stat) == 0) {
|
if (file_stat_secure(disk_path, &backup_stat)) {
|
||||||
char* backup_path = NULL;
|
|
||||||
if (backup_dir) {
|
if (backup_dir) {
|
||||||
char* resolved_backup_dir = realpath(confined_backup, NULL);
|
backup_path = path_cat(confined_backup, file->path);
|
||||||
if (!resolved_backup_dir) {
|
} else {
|
||||||
mkdir_r(confined_backup);
|
|
||||||
resolved_backup_dir = realpath(confined_backup, NULL);
|
|
||||||
}
|
|
||||||
if (resolved_backup_dir) {
|
|
||||||
char* backup_base = realpath(root_directory, NULL);
|
|
||||||
if (backup_base && path_is_within_root(backup_base, resolved_backup_dir))
|
|
||||||
backup_path = path_cat(resolved_backup_dir, file->path);
|
|
||||||
free(backup_base);
|
|
||||||
free(resolved_backup_dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!backup_path) {
|
|
||||||
size_t path_len = strlen(disk_path);
|
size_t path_len = strlen(disk_path);
|
||||||
size_t suffix_len = strlen(backup_suffix);
|
size_t suffix_len = strlen(backup_suffix);
|
||||||
|
if (path_len > SIZE_MAX - suffix_len - 1)
|
||||||
|
goto fail;
|
||||||
backup_path = malloc(path_len + suffix_len + 1);
|
backup_path = malloc(path_len + suffix_len + 1);
|
||||||
if (backup_path) {
|
if (backup_path) {
|
||||||
memcpy(backup_path, disk_path, path_len);
|
memcpy(backup_path, disk_path, path_len);
|
||||||
memcpy(backup_path + path_len, backup_suffix, suffix_len + 1);
|
memcpy(backup_path + path_len, backup_suffix, suffix_len + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (backup_path) {
|
if (!backup_path)
|
||||||
char* backup_dir_path = str_dup(backup_path);
|
goto fail;
|
||||||
if (backup_dir_path) {
|
parent_copy = str_dup(backup_path);
|
||||||
const char* bdir = dirname(backup_dir_path);
|
if (!parent_copy || !ensure_directory_secure(dirname(parent_copy)))
|
||||||
mkdir_r(bdir);
|
goto fail;
|
||||||
free(backup_dir_path);
|
free(parent_copy);
|
||||||
}
|
parent_copy = NULL;
|
||||||
if (!rename_secure(disk_path, backup_path)) {
|
if (!rename_secure(disk_path, backup_path))
|
||||||
free(backup_path);
|
goto fail;
|
||||||
free(resolved_root);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(disk_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
free(backup_path);
|
free(backup_path);
|
||||||
|
backup_path = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
char* dir_dup = str_dup(disk_path);
|
|
||||||
if (!dir_dup) {
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(resolved_root);
|
|
||||||
free(disk_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
char* dir_str = dirname(dir_dup);
|
|
||||||
if (!mkdir_r(dir_str)) {
|
|
||||||
free(dir_dup);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(resolved_root);
|
|
||||||
free(disk_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
char* resolved_dir = realpath(dir_str, NULL);
|
|
||||||
free(dir_dup);
|
|
||||||
if (resolved_dir == NULL) {
|
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to resolve directory for: %s", disk_path);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(resolved_root);
|
|
||||||
free(disk_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t root_len = strlen(resolved_root);
|
|
||||||
if (strncmp(resolved_dir, resolved_root, root_len) != 0 ||
|
|
||||||
(resolved_dir[root_len] != '\0' && resolved_dir[root_len] != '/')) {
|
|
||||||
log_message(LOG_LEVEL_ERROR, "Path escape detected: %s is outside %s", disk_path, actual_root);
|
|
||||||
free(resolved_dir);
|
|
||||||
free(confined_backup);
|
|
||||||
free(confined_partial);
|
|
||||||
free(resolved_root);
|
|
||||||
free(disk_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
free(resolved_dir);
|
|
||||||
free(resolved_root);
|
|
||||||
|
|
||||||
bool ok = to_disk_secure(disk_path, file->data->data, file->data->size, inplace, sparse,
|
bool ok = to_disk_secure(disk_path, file->data->data, file->data->size, inplace, sparse,
|
||||||
file->metadata);
|
file->metadata);
|
||||||
|
free(parent_copy);
|
||||||
|
free(backup_path);
|
||||||
free(confined_backup);
|
free(confined_backup);
|
||||||
free(confined_partial);
|
free(confined_partial);
|
||||||
free(disk_path);
|
free(disk_path);
|
||||||
return ok;
|
return ok;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
free(parent_copy);
|
||||||
|
free(backup_path);
|
||||||
|
free(confined_backup);
|
||||||
|
free(confined_partial);
|
||||||
|
free(disk_path);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static File* receive_delta_file(int fd, const Config* config, const char* check_path,
|
static File* receive_delta_file(int fd, const Config* config, const char* check_path,
|
||||||
@@ -402,7 +339,7 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (resp == STATUS_DELTA_DATA) {
|
if (resp == STATUS_DELTA_DATA) {
|
||||||
Data* delta_data = receive_data(fd);
|
Data* delta_data = receive_data_limited(fd, MAX_RECEIVE_FILE_SIZE);
|
||||||
if (!delta_data) {
|
if (!delta_data) {
|
||||||
delta_signature_destroy(sig);
|
delta_signature_destroy(sig);
|
||||||
free(old_data);
|
free(old_data);
|
||||||
@@ -412,7 +349,7 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
|
|
||||||
Data* raw_delta = delta_data;
|
Data* raw_delta = delta_data;
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
raw_delta = data_decompress(delta_data);
|
raw_delta = data_decompress_limited(delta_data, MAX_RECEIVE_FILE_SIZE);
|
||||||
data_destroy(delta_data);
|
data_destroy(delta_data);
|
||||||
if (!raw_delta) {
|
if (!raw_delta) {
|
||||||
free(old_data);
|
free(old_data);
|
||||||
@@ -431,8 +368,15 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* new_data = delta_apply(old_data, old_size, delta, config->delta_block_size);
|
|
||||||
uint64_t new_size = delta->new_file_size;
|
uint64_t new_size = delta->new_file_size;
|
||||||
|
if (new_size > MAX_RECEIVE_FILE_SIZE || new_size > SIZE_MAX) {
|
||||||
|
delta_destroy(delta);
|
||||||
|
free(old_data);
|
||||||
|
delta_signature_destroy(sig);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
void* new_data = delta_apply(old_data, old_size, delta, config->delta_block_size);
|
||||||
delta_destroy(delta);
|
delta_destroy(delta);
|
||||||
|
|
||||||
if (!new_data) {
|
if (!new_data) {
|
||||||
@@ -500,7 +444,7 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* file_data = receive_data(fd);
|
Data* file_data = receive_data_limited(fd, MAX_RECEIVE_FILE_SIZE);
|
||||||
if (file_data == NULL) {
|
if (file_data == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
send_status(fd, STATUS_ERROR);
|
send_status(fd, STATUS_ERROR);
|
||||||
@@ -508,7 +452,7 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
Data* uncompressed = data_decompress(file_data);
|
Data* uncompressed = data_decompress_limited(file_data, MAX_RECEIVE_FILE_SIZE);
|
||||||
data_destroy(file_data);
|
data_destroy(file_data);
|
||||||
if (uncompressed == NULL) {
|
if (uncompressed == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
@@ -536,6 +480,10 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
|||||||
}
|
}
|
||||||
|
|
||||||
File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
||||||
|
if (!config || !skipped) {
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
*skipped = false;
|
*skipped = false;
|
||||||
char* check_path = receive_str(fd);
|
char* check_path = receive_str(fd);
|
||||||
if (check_path == NULL) {
|
if (check_path == NULL) {
|
||||||
@@ -558,6 +506,12 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (check_size > MAX_RECEIVE_FILE_SIZE) {
|
||||||
|
free(check_path);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (has_path_traversal(check_path)) {
|
if (has_path_traversal(check_path)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Path traversal detected: %s", check_path);
|
log_message(LOG_LEVEL_ERROR, "Path traversal detected: %s", check_path);
|
||||||
free(check_path);
|
free(check_path);
|
||||||
@@ -566,10 +520,14 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* full_path = path_cat(config->receive_root_directory, check_path);
|
char* full_path = path_cat(config->receive_root_directory, check_path);
|
||||||
|
if (!full_path) {
|
||||||
|
free(check_path);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
struct stat st;
|
struct stat st;
|
||||||
bool has_old_file = false;
|
bool has_old_file = false;
|
||||||
int old_fd = -1;
|
int old_fd = -1;
|
||||||
if (full_path) {
|
|
||||||
char* leaf = NULL;
|
char* leaf = NULL;
|
||||||
int parent_fd = open_secure_parent(full_path, &leaf, false);
|
int parent_fd = open_secure_parent(full_path, &leaf, false);
|
||||||
if (parent_fd >= 0) {
|
if (parent_fd >= 0) {
|
||||||
@@ -578,10 +536,9 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
close(parent_fd);
|
close(parent_fd);
|
||||||
has_old_file = old_fd >= 0 && fstat(old_fd, &st) == 0 && S_ISREG(st.st_mode);
|
has_old_file = old_fd >= 0 && fstat(old_fd, &st) == 0 && S_ISREG(st.st_mode);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
unsigned long long old_size = has_old_file ? (unsigned long long)st.st_size : 0;
|
unsigned long long old_size = has_old_file ? (unsigned long long)st.st_size : 0;
|
||||||
void* old_data = NULL;
|
void* old_data = NULL;
|
||||||
if (has_old_file && old_size > 0 && old_size <= DELTA_MAX_FILE_SIZE && old_size <= SIZE_MAX) {
|
if (has_old_file && old_size > 0 && old_size <= MAX_RECEIVE_FILE_SIZE && old_size <= SIZE_MAX) {
|
||||||
old_data = malloc((size_t)old_size);
|
old_data = malloc((size_t)old_size);
|
||||||
if (old_data) {
|
if (old_data) {
|
||||||
size_t got = 0;
|
size_t got = 0;
|
||||||
@@ -625,7 +582,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool try_delta = config->use_delta && has_old_file &&
|
bool try_delta = config->use_delta && has_old_file && old_data != NULL &&
|
||||||
delta_should_attempt(old_size, check_size, config->delta_max_file_size);
|
delta_should_attempt(old_size, check_size, config->delta_max_file_size);
|
||||||
|
|
||||||
if (try_delta) {
|
if (try_delta) {
|
||||||
@@ -667,7 +624,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* file_data = receive_data(fd);
|
Data* file_data = receive_data_limited(fd, MAX_RECEIVE_FILE_SIZE);
|
||||||
if (file_data == NULL) {
|
if (file_data == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
send_status(fd, STATUS_ERROR);
|
send_status(fd, STATUS_ERROR);
|
||||||
@@ -675,7 +632,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
Data* uncompressed = data_decompress(file_data);
|
Data* uncompressed = data_decompress_limited(file_data, MAX_RECEIVE_FILE_SIZE);
|
||||||
data_destroy(file_data);
|
data_destroy(file_data);
|
||||||
if (uncompressed == NULL) {
|
if (uncompressed == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
@@ -708,9 +665,19 @@ static int open_secure_parent(const char* path, char** leaf_out, bool create_dir
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int fd;
|
int fd;
|
||||||
if (authorized_root_fd >= 0 && authorized_root_path && path[0] == '/' &&
|
if (authorized_root_fd >= 0) {
|
||||||
path_is_within_root(authorized_root_path, path)) {
|
if (!authorized_root_path || path[0] != '/' ||
|
||||||
|
!path_is_within_root(authorized_root_path, path)) {
|
||||||
|
free(copy);
|
||||||
|
free(leaf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
fd = dup(authorized_root_fd);
|
fd = dup(authorized_root_fd);
|
||||||
|
if (fd < 0) {
|
||||||
|
free(copy);
|
||||||
|
free(leaf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
size_t root_len = strlen(authorized_root_path);
|
size_t root_len = strlen(authorized_root_path);
|
||||||
char* relative = str_dup(path + root_len);
|
char* relative = str_dup(path + root_len);
|
||||||
if (!relative) {
|
if (!relative) {
|
||||||
@@ -734,10 +701,18 @@ static int open_secure_parent(const char* path, char** leaf_out, bool create_dir
|
|||||||
char* save = NULL;
|
char* save = NULL;
|
||||||
char* component = strtok_r(parent, "/", &save);
|
char* component = strtok_r(parent, "/", &save);
|
||||||
while (component) {
|
while (component) {
|
||||||
if (strcmp(component, ".") != 0 && strcmp(component, "..") != 0) {
|
if (strcmp(component, "..") == 0) {
|
||||||
|
close(fd);
|
||||||
|
free(copy);
|
||||||
|
free(leaf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (strcmp(component, ".") != 0) {
|
||||||
int next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
int next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
if (create_dirs && next < 0 && errno == ENOENT && mkdirat(fd, component, 0755) == 0)
|
if (create_dirs && next < 0 && errno == ENOENT) {
|
||||||
|
if (mkdirat(fd, component, 0755) == 0 || errno == EEXIST)
|
||||||
next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
|
}
|
||||||
if (next < 0) {
|
if (next < 0) {
|
||||||
close(fd);
|
close(fd);
|
||||||
free(copy);
|
free(copy);
|
||||||
@@ -754,9 +729,28 @@ static int open_secure_parent(const char* path, char** leaf_out, bool create_dir
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool ensure_directory_secure(const char* path) {
|
||||||
|
char* leaf = NULL;
|
||||||
|
int parent_fd = open_secure_parent(path, &leaf, true);
|
||||||
|
if (parent_fd < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int dir_fd = openat(parent_fd, leaf, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
|
if (dir_fd < 0 && errno == ENOENT) {
|
||||||
|
if (mkdirat(parent_fd, leaf, 0755) == 0 || errno == EEXIST)
|
||||||
|
dir_fd = openat(parent_fd, leaf, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
|
}
|
||||||
|
bool ok = dir_fd >= 0;
|
||||||
|
if (dir_fd >= 0)
|
||||||
|
close(dir_fd);
|
||||||
|
close(parent_fd);
|
||||||
|
free(leaf);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
static bool rename_secure(const char* old_path, const char* new_path) {
|
static bool rename_secure(const char* old_path, const char* new_path) {
|
||||||
char *old_leaf = NULL, *new_leaf = NULL;
|
char *old_leaf = NULL, *new_leaf = NULL;
|
||||||
int old_parent = open_secure_parent(old_path, &old_leaf, true);
|
int old_parent = open_secure_parent(old_path, &old_leaf, false);
|
||||||
int new_parent = open_secure_parent(new_path, &new_leaf, true);
|
int new_parent = open_secure_parent(new_path, &new_leaf, true);
|
||||||
bool ok = old_parent >= 0 && new_parent >= 0 &&
|
bool ok = old_parent >= 0 && new_parent >= 0 &&
|
||||||
renameat(old_parent, old_leaf, new_parent, new_leaf) == 0;
|
renameat(old_parent, old_leaf, new_parent, new_leaf) == 0;
|
||||||
@@ -833,106 +827,6 @@ bool to_disk(const char* path, const void* data, unsigned long long data_size, b
|
|||||||
if (!path || (!data && data_size != 0) || has_path_traversal(path))
|
if (!path || (!data && data_size != 0) || has_path_traversal(path))
|
||||||
return false;
|
return false;
|
||||||
return to_disk_secure(path, data, data_size, inplace, sparse, NULL);
|
return to_disk_secure(path, data, data_size, inplace, sparse, NULL);
|
||||||
/* Kept below only as historical context; all writes use descriptor-relative operations. */
|
|
||||||
char* tmp_path = NULL;
|
|
||||||
char* directory = NULL;
|
|
||||||
|
|
||||||
char* path_dup = str_dup(path);
|
|
||||||
if (!path_dup)
|
|
||||||
return false;
|
|
||||||
const char* dir_result = dirname(path_dup);
|
|
||||||
directory = str_dup(dir_result);
|
|
||||||
free(path_dup);
|
|
||||||
if (!directory)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
bool ok = true;
|
|
||||||
if (!mkdir_r(directory))
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
if (inplace) {
|
|
||||||
FILE* file_pointer = fopen(path, "wb");
|
|
||||||
if (file_pointer == NULL) {
|
|
||||||
perror("Could not open file for inplace write");
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
if (sparse && data_size > 0) {
|
|
||||||
if (fseek(file_pointer, data_size - 1, SEEK_SET) != 0) {
|
|
||||||
perror("Failed to seek for sparse file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
if (fwrite("", 1, 1, file_pointer) != 1) {
|
|
||||||
perror("Failed to write sparse file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
rewind(file_pointer);
|
|
||||||
}
|
|
||||||
if (data_size > 0 && fwrite(data, 1, data_size, file_pointer) != data_size) {
|
|
||||||
perror("Failed to write all data to file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
fclose(file_pointer);
|
|
||||||
free(directory);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t path_len = strlen(path);
|
|
||||||
tmp_path = malloc(path_len + 5);
|
|
||||||
if (!tmp_path) {
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
memcpy(tmp_path, path, path_len);
|
|
||||||
memcpy(tmp_path + path_len, ".tmp", 5);
|
|
||||||
|
|
||||||
FILE* file_pointer = fopen(tmp_path, "wb");
|
|
||||||
if (file_pointer == NULL) {
|
|
||||||
perror("Could not open temporary file");
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
if (sparse && data_size > 0) {
|
|
||||||
if (fseek(file_pointer, data_size - 1, SEEK_SET) != 0) {
|
|
||||||
perror("Failed to seek for sparse file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
if (fwrite("", 1, 1, file_pointer) != 1) {
|
|
||||||
perror("Failed to write sparse file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
rewind(file_pointer);
|
|
||||||
}
|
|
||||||
if (fwrite(data, 1, data_size, file_pointer) != data_size) {
|
|
||||||
perror("Failed to write all data to temporary file");
|
|
||||||
fclose(file_pointer);
|
|
||||||
unlink(tmp_path);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
fclose(file_pointer);
|
|
||||||
|
|
||||||
if (rename(tmp_path, path) != 0) {
|
|
||||||
perror("Failed to atomically rename temporary file");
|
|
||||||
unlink(tmp_path);
|
|
||||||
ok = false;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
done:
|
|
||||||
free(tmp_path);
|
|
||||||
free(directory);
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int compression_level,
|
bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int compression_level,
|
||||||
@@ -1044,13 +938,13 @@ File* file_receive(const Config* config, int file_descriptor) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Data* file_data = receive_data(file_descriptor);
|
Data* file_data = receive_data_limited(file_descriptor, MAX_RECEIVE_FILE_SIZE);
|
||||||
if (file_data == NULL) {
|
if (file_data == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (config->use_compression) {
|
if (config->use_compression && !compression_should_skip(file->path)) {
|
||||||
Data* file_data_uncompressed = data_decompress(file_data);
|
Data* file_data_uncompressed = data_decompress_limited(file_data, MAX_RECEIVE_FILE_SIZE);
|
||||||
data_destroy(file_data);
|
data_destroy(file_data);
|
||||||
if (file_data_uncompressed == NULL) {
|
if (file_data_uncompressed == NULL) {
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
@@ -1069,6 +963,8 @@ File* file_receive(const Config* config, int file_descriptor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t file_content_to_buffer(File* file) {
|
size_t file_content_to_buffer(File* file) {
|
||||||
|
if (!file || !file->path || !file->data || (!file->data->data && file->data->size != 0))
|
||||||
|
return 0;
|
||||||
FILE* file_pointer = fopen(file->path, "rb");
|
FILE* file_pointer = fopen(file->path, "rb");
|
||||||
if (file_pointer == NULL) {
|
if (file_pointer == NULL) {
|
||||||
perror("Could not open the file!");
|
perror("Could not open the file!");
|
||||||
@@ -1085,16 +981,26 @@ size_t file_content_to_buffer(File* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int receive_manifest(int fd, const Config* config, int* next_status) {
|
int receive_manifest(int fd, const Config* config, int* next_status) {
|
||||||
|
if (!config) {
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int received_status = STATUS_ERROR;
|
int received_status = STATUS_ERROR;
|
||||||
int* status_out = next_status ? next_status : &received_status;
|
int* status_out = next_status ? next_status : &received_status;
|
||||||
int count;
|
int count;
|
||||||
if (!receive_int(fd, &count))
|
if (!receive_int(fd, &count)) {
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
if (count < 0 || count > MAX_MANIFEST_ENTRIES)
|
}
|
||||||
|
if (count < 0 || count > MAX_MANIFEST_ENTRIES) {
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
ArrayList* manifest = array_list_create(free);
|
ArrayList* manifest = array_list_create(free);
|
||||||
if (!manifest)
|
if (!manifest) {
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
size_t manifest_bytes = 0;
|
size_t manifest_bytes = 0;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
char* s = receive_str(fd);
|
char* s = receive_str(fd);
|
||||||
@@ -1104,22 +1010,28 @@ int receive_manifest(int fd, const Config* config, int* next_status) {
|
|||||||
(manifest_bytes += entry_size) > MAX_MANIFEST_BYTES || !array_list_add(manifest, s)) {
|
(manifest_bytes += entry_size) > MAX_MANIFEST_BYTES || !array_list_add(manifest, s)) {
|
||||||
free(s);
|
free(s);
|
||||||
array_list_delete(manifest);
|
array_list_delete(manifest);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!receive_status(fd, status_out)) {
|
if (!receive_status(fd, status_out)) {
|
||||||
array_list_delete(manifest);
|
array_list_delete(manifest);
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* Deletion is a commit operation: never perform it until the sender has
|
/* Deletion is a commit operation: never perform it until the sender has
|
||||||
completed the manifest frame successfully. */
|
completed the manifest frame successfully. */
|
||||||
if (*status_out != STATUS_FINISHED || !config->use_delete) {
|
if (*status_out != STATUS_FINISHED || !config->use_delete) {
|
||||||
array_list_delete(manifest);
|
array_list_delete(manifest);
|
||||||
|
if (*status_out != STATUS_FINISHED)
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return *status_out == STATUS_FINISHED ? 0 : -1;
|
return *status_out == STATUS_FINISHED ? 0 : -1;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "Deleting files not in manifest...\n");
|
fprintf(stderr, "Deleting files not in manifest...\n");
|
||||||
bool deletion_ok =
|
bool deletion_ok =
|
||||||
delete_extras_limited(config->receive_root_directory, manifest, MAX_SERVER_DELETE_COUNT);
|
delete_extras_limited(config->receive_root_directory, manifest, MAX_SERVER_DELETE_COUNT);
|
||||||
array_list_delete(manifest);
|
array_list_delete(manifest);
|
||||||
|
if (!deletion_ok)
|
||||||
|
send_status(fd, STATUS_ERROR);
|
||||||
return deletion_ok ? 0 : -1;
|
return deletion_ok ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -38,7 +38,8 @@ void file_metadata_destroy(void* metadata);
|
|||||||
bool to_disk(const char* path, const void* data, unsigned long long data_size, bool inplace,
|
bool to_disk(const char* path, const void* data, unsigned long long data_size, bool inplace,
|
||||||
bool sparse);
|
bool sparse);
|
||||||
bool file_save_to_disk(const char* root_directory, const File* file, const Config* config);
|
bool file_save_to_disk(const char* root_directory, const File* file, const Config* config);
|
||||||
void file_set_authorized_root(int fd, const char* canonical_path);
|
/* A configured fd without a canonical identity deliberately rejects paths. */
|
||||||
|
bool file_set_authorized_root(int fd, const char* canonical_path);
|
||||||
File* receive_incremental_check(int fd, const Config* config, bool* skipped);
|
File* receive_incremental_check(int fd, const Config* config, bool* skipped);
|
||||||
bool file_path_exists_secure(const char* path);
|
bool file_path_exists_secure(const char* path);
|
||||||
bool file_stat_secure(const char* path, struct stat* st);
|
bool file_stat_secure(const char* path, struct stat* st);
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ FileMetadata* metadata_from_buf(char** buf) {
|
|||||||
int32_t present;
|
int32_t present;
|
||||||
memcpy(&present, *buf, sizeof(present));
|
memcpy(&present, *buf, sizeof(present));
|
||||||
*buf += sizeof(present);
|
*buf += sizeof(present);
|
||||||
|
if (present != 0 && present != 1)
|
||||||
|
return NULL;
|
||||||
if (!present)
|
if (!present)
|
||||||
return NULL;
|
return NULL;
|
||||||
FileMetadata* m = malloc(sizeof(FileMetadata));
|
FileMetadata* m = malloc(sizeof(FileMetadata));
|
||||||
@@ -84,7 +86,7 @@ FileMetadata* metadata_from_buf(char** buf) {
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool metadata_send(int file_descriptor, FileMetadata* m) {
|
bool metadata_send(int file_descriptor, const FileMetadata* m) {
|
||||||
if (m == NULL) {
|
if (m == NULL) {
|
||||||
int32_t zero = 0;
|
int32_t zero = 0;
|
||||||
return send_n_data(file_descriptor, &zero, sizeof(zero));
|
return send_n_data(file_descriptor, &zero, sizeof(zero));
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
void metadata_to_buf(char** buf, const FileMetadata* m);
|
void metadata_to_buf(char** buf, const FileMetadata* m);
|
||||||
FileMetadata* metadata_from_buf(char** buf);
|
FileMetadata* metadata_from_buf(char** buf);
|
||||||
bool metadata_send(int file_descriptor, FileMetadata* m);
|
bool metadata_send(int file_descriptor, const FileMetadata* m);
|
||||||
FileMetadata* metadata_receive(int file_descriptor, int* ok);
|
FileMetadata* metadata_receive(int file_descriptor, int* ok);
|
||||||
void file_restore_metadata(const char* path, const FileMetadata* metadata);
|
void file_restore_metadata(const char* path, const FileMetadata* metadata);
|
||||||
bool file_restore_metadata_fd(int fd, const FileMetadata* metadata);
|
bool file_restore_metadata_fd(int fd, const FileMetadata* metadata);
|
||||||
|
|||||||
@@ -233,6 +233,12 @@ int receive_thread(void* pipeline_context) {
|
|||||||
RECEIVE_THREAD_FAIL();
|
RECEIVE_THREAD_FAIL();
|
||||||
}
|
}
|
||||||
char* full_path = path_cat(config->receive_root_directory, check_path);
|
char* full_path = path_cat(config->receive_root_directory, check_path);
|
||||||
|
if (!full_path) {
|
||||||
|
free(check_path);
|
||||||
|
if (!send_status(file_descriptor, STATUS_ERROR))
|
||||||
|
RECEIVE_THREAD_FAIL();
|
||||||
|
RECEIVE_THREAD_FAIL();
|
||||||
|
}
|
||||||
struct stat st;
|
struct stat st;
|
||||||
bool has_old = full_path && file_stat_secure(full_path, &st);
|
bool has_old = full_path && file_stat_secure(full_path, &st);
|
||||||
bool match = has_old && (unsigned long long)st.st_size == check_size &&
|
bool match = has_old && (unsigned long long)st.st_size == check_size &&
|
||||||
@@ -263,9 +269,10 @@ int receive_thread(void* pipeline_context) {
|
|||||||
RECEIVE_THREAD_FAIL();
|
RECEIVE_THREAD_FAIL();
|
||||||
}
|
}
|
||||||
if (status == STATUS_MANIFEST) {
|
if (status == STATUS_MANIFEST) {
|
||||||
if (receive_manifest(file_descriptor, config, &status) != 0)
|
if (receive_manifest(file_descriptor, config, &status) != 0) {
|
||||||
RECEIVE_THREAD_FAIL();
|
RECEIVE_THREAD_FAIL();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (status != STATUS_FINISHED)
|
if (status != STATUS_FINISHED)
|
||||||
RECEIVE_THREAD_FAIL();
|
RECEIVE_THREAD_FAIL();
|
||||||
mtx_lock(&context->mutex);
|
mtx_lock(&context->mutex);
|
||||||
|
|||||||
+44
-12
@@ -27,6 +27,13 @@ static once_flag bw_mutex_once = ONCE_FLAG_INIT;
|
|||||||
|
|
||||||
static __thread unsigned long long total_allocated_bytes;
|
static __thread unsigned long long total_allocated_bytes;
|
||||||
|
|
||||||
|
void protocol_release_memory(size_t charge) {
|
||||||
|
if ((unsigned long long)charge >= total_allocated_bytes)
|
||||||
|
total_allocated_bytes = 0;
|
||||||
|
else
|
||||||
|
total_allocated_bytes -= charge;
|
||||||
|
}
|
||||||
|
|
||||||
void io_set_fds(int read_fd, int write_fd) {
|
void io_set_fds(int read_fd, int write_fd) {
|
||||||
io_read_fd = read_fd;
|
io_read_fd = read_fd;
|
||||||
io_write_fd = write_fd;
|
io_write_fd = write_fd;
|
||||||
@@ -43,17 +50,20 @@ static void bw_mutex_init(void) {
|
|||||||
void io_set_bwlimit(unsigned long long bytes_per_sec) {
|
void io_set_bwlimit(unsigned long long bytes_per_sec) {
|
||||||
call_once(&bw_mutex_once, bw_mutex_init);
|
call_once(&bw_mutex_once, bw_mutex_init);
|
||||||
mtx_lock(&bw_mutex);
|
mtx_lock(&bw_mutex);
|
||||||
io_bwlimit = bytes_per_sec;
|
io_bwlimit =
|
||||||
|
bytes_per_sec > (unsigned long long)LLONG_MAX ? (unsigned long long)LLONG_MAX : bytes_per_sec;
|
||||||
bw_tokens = (long long)io_bwlimit;
|
bw_tokens = (long long)io_bwlimit;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &bw_last_refill);
|
clock_gettime(CLOCK_MONOTONIC, &bw_last_refill);
|
||||||
mtx_unlock(&bw_mutex);
|
mtx_unlock(&bw_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bw_throttle(size_t bytes_written) {
|
static void bw_throttle(size_t bytes_written) {
|
||||||
if (io_bwlimit == 0)
|
|
||||||
return;
|
|
||||||
call_once(&bw_mutex_once, bw_mutex_init);
|
call_once(&bw_mutex_once, bw_mutex_init);
|
||||||
mtx_lock(&bw_mutex);
|
mtx_lock(&bw_mutex);
|
||||||
|
if (io_bwlimit == 0) {
|
||||||
|
mtx_unlock(&bw_mutex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
@@ -81,6 +91,15 @@ static void bw_throttle(size_t bytes_written) {
|
|||||||
mtx_unlock(&bw_mutex);
|
mtx_unlock(&bw_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool bw_enabled(void) {
|
||||||
|
bool enabled;
|
||||||
|
call_once(&bw_mutex_once, bw_mutex_init);
|
||||||
|
mtx_lock(&bw_mutex);
|
||||||
|
enabled = io_bwlimit != 0;
|
||||||
|
mtx_unlock(&bw_mutex);
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
void io_set_ssl(SSL* ssl) {
|
void io_set_ssl(SSL* ssl) {
|
||||||
io_ssl = ssl;
|
io_ssl = ssl;
|
||||||
}
|
}
|
||||||
@@ -105,6 +124,8 @@ static int deadline_remaining_ms(const struct timespec* deadline) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool send_n_data(int file_descriptor, const void* data, size_t data_size) {
|
bool send_n_data(int file_descriptor, const void* data, size_t data_size) {
|
||||||
|
if (!data && data_size != 0)
|
||||||
|
return false;
|
||||||
log_message(LOG_LEVEL_DEBUG, " Sending n Data: %zu", data_size);
|
log_message(LOG_LEVEL_DEBUG, " Sending n Data: %zu", data_size);
|
||||||
int fd = io_fd(io_write_fd, file_descriptor);
|
int fd = io_fd(io_write_fd, file_descriptor);
|
||||||
struct timespec deadline;
|
struct timespec deadline;
|
||||||
@@ -114,7 +135,7 @@ bool send_n_data(int file_descriptor, const void* data, size_t data_size) {
|
|||||||
ssize_t total_bytes_send = 0;
|
ssize_t total_bytes_send = 0;
|
||||||
while ((size_t)total_bytes_send < data_size) {
|
while ((size_t)total_bytes_send < data_size) {
|
||||||
size_t chunk = data_size - total_bytes_send;
|
size_t chunk = data_size - total_bytes_send;
|
||||||
if (io_bwlimit > 0 && chunk > 65536)
|
if (bw_enabled() && chunk > 65536)
|
||||||
chunk = 65536;
|
chunk = 65536;
|
||||||
struct pollfd pfd = {.fd = fd, .events = wait_events};
|
struct pollfd pfd = {.fd = fd, .events = wait_events};
|
||||||
int poll_result = poll(&pfd, 1, deadline_remaining_ms(&deadline));
|
int poll_result = poll(&pfd, 1, deadline_remaining_ms(&deadline));
|
||||||
@@ -144,6 +165,8 @@ bool send_n_data(int file_descriptor, const void* data, size_t data_size) {
|
|||||||
}
|
}
|
||||||
bw_throttle((size_t)bytes_send);
|
bw_throttle((size_t)bytes_send);
|
||||||
total_bytes_send += bytes_send;
|
total_bytes_send += bytes_send;
|
||||||
|
if (io_ssl)
|
||||||
|
wait_events = POLLOUT;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_DEBUG, " Send n Data: %zu", total_bytes_send);
|
log_message(LOG_LEVEL_DEBUG, " Send n Data: %zu", total_bytes_send);
|
||||||
return true;
|
return true;
|
||||||
@@ -160,6 +183,7 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) {
|
|||||||
size_t total_bytes_received = 0;
|
size_t total_bytes_received = 0;
|
||||||
short wait_events = POLLIN;
|
short wait_events = POLLIN;
|
||||||
while (total_bytes_received < data_size) {
|
while (total_bytes_received < data_size) {
|
||||||
|
if (!io_ssl || SSL_pending(io_ssl) == 0) {
|
||||||
struct pollfd pfd = {.fd = fd, .events = wait_events};
|
struct pollfd pfd = {.fd = fd, .events = wait_events};
|
||||||
int poll_result = poll(&pfd, 1, deadline_remaining_ms(&deadline));
|
int poll_result = poll(&pfd, 1, deadline_remaining_ms(&deadline));
|
||||||
if (poll_result == 0) {
|
if (poll_result == 0) {
|
||||||
@@ -174,6 +198,7 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) {
|
|||||||
/* POLLHUP may accompany the final readable bytes on pipes/sockets. */
|
/* POLLHUP may accompany the final readable bytes on pipes/sockets. */
|
||||||
if (pfd.revents & (POLLERR | POLLNVAL))
|
if (pfd.revents & (POLLERR | POLLNVAL))
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t bytes_received;
|
ssize_t bytes_received;
|
||||||
if (io_ssl)
|
if (io_ssl)
|
||||||
@@ -196,7 +221,9 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) {
|
|||||||
log_message(LOG_LEVEL_ERROR, "Could not receive bytes");
|
log_message(LOG_LEVEL_ERROR, "Could not receive bytes");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
total_bytes_received += bytes_received;
|
total_bytes_received += (size_t)bytes_received;
|
||||||
|
if (io_ssl)
|
||||||
|
wait_events = POLLIN;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_DEBUG, " Received n Data: %zu", total_bytes_received);
|
log_message(LOG_LEVEL_DEBUG, " Received n Data: %zu", total_bytes_received);
|
||||||
return true;
|
return true;
|
||||||
@@ -247,8 +274,7 @@ char* receive_str(int file_descriptor) {
|
|||||||
size_t size;
|
size_t size;
|
||||||
if (!receive_n_data(file_descriptor, &size, sizeof(size_t)))
|
if (!receive_n_data(file_descriptor, &size, sizeof(size_t)))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (size > MAX_STRING_SIZE || size > SIZE_MAX - 1 ||
|
if (size > MAX_STRING_SIZE || size > SIZE_MAX - 1) {
|
||||||
size + 1 > MAX_CONNECTION_MEMORY - total_allocated_bytes) {
|
|
||||||
log_message(LOG_LEVEL_ERROR, "String size %zu exceeds maximum %llu", size,
|
log_message(LOG_LEVEL_ERROR, "String size %zu exceeds maximum %llu", size,
|
||||||
(unsigned long long)MAX_STRING_SIZE);
|
(unsigned long long)MAX_STRING_SIZE);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -266,12 +292,13 @@ char* receive_str(int file_descriptor) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
data[size] = '\0';
|
data[size] = '\0';
|
||||||
total_allocated_bytes += size + 1;
|
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received String: %s", data);
|
log_message(LOG_LEVEL_DEBUG, "Received String: %s", data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool send_data(int file_descriptor, const Data* data) {
|
bool send_data(int file_descriptor, const Data* data) {
|
||||||
|
if (!data || (!data->data && data->size != 0))
|
||||||
|
return false;
|
||||||
unsigned long long data_size = data->size;
|
unsigned long long data_size = data->size;
|
||||||
if (!send_n_data(file_descriptor, &data_size, sizeof(unsigned long long)))
|
if (!send_n_data(file_descriptor, &data_size, sizeof(unsigned long long)))
|
||||||
return false;
|
return false;
|
||||||
@@ -281,11 +308,11 @@ bool send_data(int file_descriptor, const Data* data) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data* receive_data(int file_descriptor) {
|
Data* receive_data_limited(int file_descriptor, unsigned long long maximum_size) {
|
||||||
unsigned long long size = 0;
|
unsigned long long size = 0;
|
||||||
if (!receive_n_data(file_descriptor, &size, sizeof(unsigned long long)))
|
if (!receive_n_data(file_descriptor, &size, sizeof(unsigned long long)))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (size > MAX_DATA_PAYLOAD_SIZE) {
|
if (size > MAX_DATA_PAYLOAD_SIZE || size > maximum_size) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Data size %llu exceeds maximum %llu", size,
|
log_message(LOG_LEVEL_ERROR, "Data size %llu exceeds maximum %llu", size,
|
||||||
(unsigned long long)MAX_DATA_PAYLOAD_SIZE);
|
(unsigned long long)MAX_DATA_PAYLOAD_SIZE);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -302,16 +329,21 @@ Data* receive_data(int file_descriptor) {
|
|||||||
free(data);
|
free(data);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
total_allocated_bytes += allocation_size;
|
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received %lld data", size);
|
log_message(LOG_LEVEL_DEBUG, "Received %lld data", size);
|
||||||
Data* result = data_create(data, (size_t)size);
|
Data* result = data_create(data, (size_t)size);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
free(data);
|
free(data);
|
||||||
total_allocated_bytes -= allocation_size;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
total_allocated_bytes += allocation_size;
|
||||||
|
result->protocol_charge = allocation_size;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Data* receive_data(int file_descriptor) {
|
||||||
|
return receive_data_limited(file_descriptor, MAX_DATA_PAYLOAD_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
bool send_int(int file_descriptor, int data) {
|
bool send_int(int file_descriptor, int data) {
|
||||||
if (!send_n_data(file_descriptor, &data, sizeof(int)))
|
if (!send_n_data(file_descriptor, &data, sizeof(int)))
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
/* Maximum allowed data payload size for receive_data (100 MB) */
|
/* Maximum allowed data payload size for receive_data (100 MB) */
|
||||||
#define MAX_DATA_PAYLOAD_SIZE (100ULL * 1024 * 1024)
|
#define MAX_DATA_PAYLOAD_SIZE (100ULL * 1024 * 1024)
|
||||||
|
/* Maximum uncompressed file payload accepted by the receiver. */
|
||||||
|
#define MAX_RECEIVE_FILE_SIZE (64ULL * 1024 * 1024)
|
||||||
|
|
||||||
/* Maximum chunk size (64 MB) — prevents unbounded allocation from the wire */
|
/* Maximum chunk size (64 MB) — prevents unbounded allocation from the wire */
|
||||||
#define MAX_CHUNK_SIZE (64ULL * 1024 * 1024)
|
#define MAX_CHUNK_SIZE (64ULL * 1024 * 1024)
|
||||||
@@ -46,6 +48,7 @@ bool send_str(int file_descriptor, const char* data);
|
|||||||
char* receive_str(int file_descriptor);
|
char* receive_str(int file_descriptor);
|
||||||
bool send_data(int file_descriptor, const Data* data);
|
bool send_data(int file_descriptor, const Data* data);
|
||||||
Data* receive_data(int file_descriptor);
|
Data* receive_data(int file_descriptor);
|
||||||
|
Data* receive_data_limited(int file_descriptor, unsigned long long maximum_size);
|
||||||
bool send_int(int file_descriptor, int data);
|
bool send_int(int file_descriptor, int data);
|
||||||
bool receive_int(int file_descriptor, int* data);
|
bool receive_int(int file_descriptor, int* data);
|
||||||
bool send_status(int file_descriptor, Status status);
|
bool send_status(int file_descriptor, Status status);
|
||||||
|
|||||||
@@ -105,7 +105,10 @@ static SSL* wrap_fd_with_ssl(int fd, SSL_CTX* ctx, bool is_server, const char* h
|
|||||||
log_message(LOG_LEVEL_ERROR, "Failed to create SSL object");
|
log_message(LOG_LEVEL_ERROR, "Failed to create SSL object");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
SSL_set_fd(ssl, fd);
|
if (SSL_set_fd(ssl, fd) != 1) {
|
||||||
|
SSL_free(ssl);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Enable hostname verification for client connections when a hostname is provided.
|
// Enable hostname verification for client connections when a hostname is provided.
|
||||||
// Must be done before SSL_connect to take effect during the handshake.
|
// Must be done before SSL_connect to take effect during the handshake.
|
||||||
@@ -154,8 +157,10 @@ struct tls_child_ctx {
|
|||||||
static void tls_child_fn(int fd, void* arg) {
|
static void tls_child_fn(int fd, void* arg) {
|
||||||
struct tls_child_ctx* ctx = (struct tls_child_ctx*)arg;
|
struct tls_child_ctx* ctx = (struct tls_child_ctx*)arg;
|
||||||
SSL* ssl = wrap_fd_with_ssl(fd, ctx->ssl_ctx, true, NULL);
|
SSL* ssl = wrap_fd_with_ssl(fd, ctx->ssl_ctx, true, NULL);
|
||||||
if (!ssl)
|
if (!ssl) {
|
||||||
|
io_set_ssl(NULL);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
io_set_ssl(ssl);
|
io_set_ssl(ssl);
|
||||||
ctx->handler(fd);
|
ctx->handler(fd);
|
||||||
SSL_shutdown(ssl);
|
SSL_shutdown(ssl);
|
||||||
@@ -184,6 +189,10 @@ bool client_connect_tls(Client* client, char* host, int port, const char* cert_p
|
|||||||
int err = getaddrinfo(host, port_str, &hints, &result);
|
int err = getaddrinfo(host, port_str, &hints, &result);
|
||||||
if (err != 0 || result == NULL) {
|
if (err != 0 || result == NULL) {
|
||||||
fprintf(stderr, "Could not resolve host: %s (%s)\n", host, gai_strerror(err));
|
fprintf(stderr, "Could not resolve host: %s (%s)\n", host, gai_strerror(err));
|
||||||
|
if (client->file_descriptor >= 0) {
|
||||||
|
close(client->file_descriptor);
|
||||||
|
client->file_descriptor = -1;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,12 +225,18 @@ bool client_connect_tls(Client* client, char* host, int port, const char* cert_p
|
|||||||
|
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
perror("Could not connect to Server!");
|
perror("Could not connect to Server!");
|
||||||
|
if (client->file_descriptor >= 0)
|
||||||
|
close(client->file_descriptor);
|
||||||
|
client->file_descriptor = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SSL_CTX* ctx = create_ssl_ctx(false, cert_path, key_path, ca_path);
|
SSL_CTX* ctx = create_ssl_ctx(false, cert_path, key_path, ca_path);
|
||||||
if (!ctx)
|
if (!ctx) {
|
||||||
|
close(client->file_descriptor);
|
||||||
|
client->file_descriptor = -1;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
client->ssl_ctx = ctx;
|
client->ssl_ctx = ctx;
|
||||||
|
|
||||||
// Pass the server hostname for TLS hostname verification (SSL_set1_host
|
// Pass the server hostname for TLS hostname verification (SSL_set1_host
|
||||||
@@ -231,6 +246,8 @@ bool client_connect_tls(Client* client, char* host, int port, const char* cert_p
|
|||||||
if (!ssl) {
|
if (!ssl) {
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
client->ssl_ctx = NULL;
|
client->ssl_ctx = NULL;
|
||||||
|
close(client->file_descriptor);
|
||||||
|
client->file_descriptor = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+70
-52
@@ -14,14 +14,22 @@
|
|||||||
static int authorized_root_fd = -1;
|
static int authorized_root_fd = -1;
|
||||||
static char* authorized_root_path;
|
static char* authorized_root_path;
|
||||||
|
|
||||||
void utils_set_authorized_root(int fd, const char* canonical_path) {
|
bool utils_set_authorized_root(int fd, const char* canonical_path) {
|
||||||
|
char* path_copy = canonical_path ? str_dup(canonical_path) : NULL;
|
||||||
|
if (canonical_path && !path_copy) {
|
||||||
|
authorized_root_fd = -1;
|
||||||
|
free(authorized_root_path);
|
||||||
|
authorized_root_path = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
authorized_root_fd = fd;
|
authorized_root_fd = fd;
|
||||||
free(authorized_root_path);
|
free(authorized_root_path);
|
||||||
authorized_root_path = canonical_path ? str_dup(canonical_path) : NULL;
|
authorized_root_path = path_copy;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void utils_set_authorized_root_fd(int fd) {
|
void utils_set_authorized_root_fd(int fd) {
|
||||||
utils_set_authorized_root(fd, NULL);
|
(void)utils_set_authorized_root(fd, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool path_is_within_root(const char* root, const char* path) {
|
static bool path_is_within_root(const char* root, const char* path) {
|
||||||
@@ -50,11 +58,15 @@ static int open_authorized_destination(const char* dest_root) {
|
|||||||
char* saveptr = NULL;
|
char* saveptr = NULL;
|
||||||
char* component = strtok_r(relative, "/", &saveptr);
|
char* component = strtok_r(relative, "/", &saveptr);
|
||||||
while (component) {
|
while (component) {
|
||||||
if (strcmp(component, ".") == 0 || strcmp(component, "..") == 0) {
|
if (strcmp(component, "..") == 0) {
|
||||||
free(relative);
|
free(relative);
|
||||||
close(dirfd);
|
close(dirfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (strcmp(component, ".") == 0) {
|
||||||
|
component = strtok_r(NULL, "/", &saveptr);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
int next = openat(dirfd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
int next = openat(dirfd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
if (next < 0) {
|
if (next < 0) {
|
||||||
free(relative);
|
free(relative);
|
||||||
@@ -71,52 +83,41 @@ static int open_authorized_destination(const char* dest_root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool mkdir_r(const char* path) {
|
bool mkdir_r(const char* path) {
|
||||||
size_t path_len = strlen(path);
|
if (!path || *path == '\0')
|
||||||
char* path_duplicate = malloc(path_len + 1);
|
|
||||||
if (!path_duplicate)
|
|
||||||
return false;
|
return false;
|
||||||
memcpy(path_duplicate, path, path_len + 1);
|
char* duplicate = str_dup(path);
|
||||||
size_t capacity = path_len + 2;
|
if (!duplicate)
|
||||||
char* path_current = (char*)malloc(capacity * sizeof(char));
|
return false;
|
||||||
if (!path_current) {
|
int dirfd = open(path[0] == '/' ? "/" : ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
|
||||||
free(path_duplicate);
|
if (dirfd < 0) {
|
||||||
|
free(duplicate);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
char* path_current_position = path_current;
|
|
||||||
if (path[0] == '/') {
|
|
||||||
path_current[0] = '/';
|
|
||||||
path_current[1] = '\0';
|
|
||||||
path_current_position += 1;
|
|
||||||
} else {
|
|
||||||
path_current[0] = '\0';
|
|
||||||
}
|
|
||||||
const char* delimiter = "/";
|
|
||||||
char* saveptr;
|
|
||||||
const char* part = strtok_r(path_duplicate, delimiter, &saveptr);
|
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
while (part != NULL) {
|
char* saveptr = NULL;
|
||||||
size_t part_len = strlen(part);
|
char* component = strtok_r(duplicate, "/", &saveptr);
|
||||||
if ((size_t)(path_current_position - path_current) + part_len + 2 > capacity) {
|
while (component) {
|
||||||
|
if (strcmp(component, "..") == 0) {
|
||||||
ok = false;
|
ok = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
memcpy(path_current_position, part, part_len);
|
if (strcmp(component, ".") != 0) {
|
||||||
path_current_position += part_len;
|
int next = openat(dirfd, component, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
|
||||||
path_current_position[0] = '/';
|
if (next < 0 && errno == ENOENT) {
|
||||||
path_current_position[1] = '\0';
|
if (mkdirat(dirfd, component, 0755) == 0 || errno == EEXIST)
|
||||||
path_current_position++;
|
next = openat(dirfd, component, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
|
||||||
struct stat st;
|
}
|
||||||
if (stat(path_current, &st) != 0) {
|
if (next < 0) {
|
||||||
if (mkdir(path_current, 0755) != 0) {
|
|
||||||
perror("Could not create directory");
|
|
||||||
ok = false;
|
ok = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
close(dirfd);
|
||||||
|
dirfd = next;
|
||||||
}
|
}
|
||||||
part = strtok_r(NULL, delimiter, &saveptr);
|
component = strtok_r(NULL, "/", &saveptr);
|
||||||
}
|
}
|
||||||
free(path_duplicate);
|
close(dirfd);
|
||||||
free(path_current);
|
free(duplicate);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,15 +208,20 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
|
|||||||
close(scanfd);
|
close(scanfd);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool all_removed = true;
|
|
||||||
bool operation_ok = true;
|
bool operation_ok = true;
|
||||||
const struct dirent* entry;
|
const struct dirent* entry;
|
||||||
while ((entry = readdir(dir)) != NULL) {
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
char* child_rel = path_cat((char*)rel_path, entry->d_name);
|
char* child_rel = path_cat((char*)rel_path, entry->d_name);
|
||||||
|
if (!child_rel) {
|
||||||
|
operation_ok = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (fstatat(dirfd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) {
|
if (fstatat(dirfd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) {
|
||||||
|
if (errno != ENOENT)
|
||||||
|
operation_ok = false;
|
||||||
free(child_rel);
|
free(child_rel);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -229,18 +235,23 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
|
|||||||
bool child_removed = false;
|
bool child_removed = false;
|
||||||
if (childfd >= 0) {
|
if (childfd >= 0) {
|
||||||
child_removed = delete_extras_fd(childfd, child_rel, manifest, max_delete, deleted_count);
|
child_removed = delete_extras_fd(childfd, child_rel, manifest, max_delete, deleted_count);
|
||||||
|
if (!child_removed)
|
||||||
|
operation_ok = false;
|
||||||
close(childfd);
|
close(childfd);
|
||||||
|
} else if (errno != ENOENT) {
|
||||||
|
operation_ok = false;
|
||||||
}
|
}
|
||||||
if (child_removed && !is_dir_in_manifest(child_rel, manifest)) {
|
if (child_removed && !is_dir_in_manifest(child_rel, manifest)) {
|
||||||
if (*deleted_count >= max_delete) {
|
if (*deleted_count >= max_delete) {
|
||||||
operation_ok = false;
|
operation_ok = false;
|
||||||
} else if (unlinkat(dirfd, entry->d_name, AT_REMOVEDIR) != 0 && errno != ENOENT) {
|
} else {
|
||||||
|
if (unlinkat(dirfd, entry->d_name, AT_REMOVEDIR) != 0) {
|
||||||
|
if (errno != ENOENT)
|
||||||
operation_ok = false;
|
operation_ok = false;
|
||||||
} else {
|
} else {
|
||||||
(*deleted_count)++;
|
(*deleted_count)++;
|
||||||
}
|
}
|
||||||
} else if (!child_removed) {
|
}
|
||||||
all_removed = false;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check if relative path is in manifest
|
// Check if relative path is in manifest
|
||||||
@@ -257,27 +268,32 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
|
|||||||
free(child_rel);
|
free(child_rel);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (unlinkat(dirfd, entry->d_name, 0) != 0 && errno != ENOENT)
|
if (unlinkat(dirfd, entry->d_name, 0) != 0) {
|
||||||
|
if (errno != ENOENT)
|
||||||
operation_ok = false;
|
operation_ok = false;
|
||||||
else
|
|
||||||
(*deleted_count)++;
|
|
||||||
fprintf(stderr, " Deleted: %s\n", child_rel);
|
|
||||||
} else {
|
} else {
|
||||||
all_removed = false;
|
(*deleted_count)++;
|
||||||
|
}
|
||||||
|
fprintf(stderr, " Deleted: %s\n", child_rel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(child_rel);
|
free(child_rel);
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
(void)all_removed;
|
|
||||||
return operation_ok;
|
return operation_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool delete_extras_limited(const char* dest_root, ArrayList* manifest, size_t max_delete) {
|
bool delete_extras_limited(const char* dest_root, ArrayList* manifest, size_t max_delete) {
|
||||||
|
if (!manifest)
|
||||||
|
return false;
|
||||||
int rootfd;
|
int rootfd;
|
||||||
if (authorized_root_fd >= 0) {
|
if (authorized_root_fd >= 0) {
|
||||||
rootfd =
|
if (authorized_root_path)
|
||||||
authorized_root_path ? open_authorized_destination(dest_root) : dup(authorized_root_fd);
|
rootfd = open_authorized_destination(dest_root);
|
||||||
|
else if (dest_root == NULL)
|
||||||
|
rootfd = dup(authorized_root_fd);
|
||||||
|
else
|
||||||
|
rootfd = -1;
|
||||||
} else {
|
} else {
|
||||||
rootfd = open(dest_root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
rootfd = open(dest_root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
}
|
}
|
||||||
@@ -296,10 +312,10 @@ bool delete_extras(const char* dest_root, ArrayList* manifest) {
|
|||||||
|
|
||||||
bool has_path_traversal(const char* path) {
|
bool has_path_traversal(const char* path) {
|
||||||
if (!path)
|
if (!path)
|
||||||
return false;
|
return true;
|
||||||
char* dup = str_dup(path);
|
char* dup = str_dup(path);
|
||||||
if (!dup)
|
if (!dup)
|
||||||
return false;
|
return true;
|
||||||
char* saveptr;
|
char* saveptr;
|
||||||
const char* part = strtok_r(dup, "/", &saveptr);
|
const char* part = strtok_r(dup, "/", &saveptr);
|
||||||
while (part) {
|
while (part) {
|
||||||
@@ -327,6 +343,8 @@ char* path_cat(const char* path1, const char* path2) {
|
|||||||
offset = 1;
|
offset = 1;
|
||||||
path2_len -= 1;
|
path2_len -= 1;
|
||||||
}
|
}
|
||||||
|
if (path1_len > SIZE_MAX - path2_len - 2)
|
||||||
|
return NULL;
|
||||||
char* new_path = malloc(path1_len + path2_len + 2);
|
char* new_path = malloc(path1_len + path2_len + 2);
|
||||||
if (new_path == NULL)
|
if (new_path == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
+3
-1
@@ -11,7 +11,9 @@ char* path_cat(const char* path1, const char* path2);
|
|||||||
bool glob_match(const char* pattern, const char* str);
|
bool glob_match(const char* pattern, const char* str);
|
||||||
bool delete_extras(const char* dest_root, ArrayList* manifest);
|
bool delete_extras(const char* dest_root, ArrayList* manifest);
|
||||||
bool delete_extras_limited(const char* dest_root, ArrayList* manifest, size_t max_delete);
|
bool delete_extras_limited(const char* dest_root, ArrayList* manifest, size_t max_delete);
|
||||||
void utils_set_authorized_root(int fd, const char* canonical_path);
|
bool utils_set_authorized_root(int fd, const char* canonical_path);
|
||||||
|
/* The fd-only compatibility form is fail-closed for path-based operations;
|
||||||
|
* callers should use utils_set_authorized_root with the canonical identity. */
|
||||||
void utils_set_authorized_root_fd(int fd);
|
void utils_set_authorized_root_fd(int fd);
|
||||||
bool has_path_traversal(const char* path);
|
bool has_path_traversal(const char* path);
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,15 @@ static void test_destroyer(void* item) {
|
|||||||
void test_array_list() {
|
void test_array_list() {
|
||||||
ArrayList* list = array_list_create(free);
|
ArrayList* list = array_list_create(free);
|
||||||
EXPECT_NOT_NULL(list);
|
EXPECT_NOT_NULL(list);
|
||||||
|
if (!list)
|
||||||
|
return;
|
||||||
EXPECT_EQ_INT(list->size, 0);
|
EXPECT_EQ_INT(list->size, 0);
|
||||||
EXPECT_EQ_INT(list->capacity, 100);
|
EXPECT_EQ_INT(list->capacity, 100);
|
||||||
|
|
||||||
// Test adding
|
// Test adding
|
||||||
int* val1 = malloc(sizeof(int));
|
int* val1 = malloc(sizeof(int));
|
||||||
|
if (!val1)
|
||||||
|
return;
|
||||||
*val1 = 42;
|
*val1 = 42;
|
||||||
array_list_add(list, val1);
|
array_list_add(list, val1);
|
||||||
EXPECT_EQ_INT(list->size, 1);
|
EXPECT_EQ_INT(list->size, 1);
|
||||||
@@ -26,6 +30,8 @@ void test_array_list() {
|
|||||||
// Initial capacity is 100. Let's add 105 elements.
|
// Initial capacity is 100. Let's add 105 elements.
|
||||||
for (int i = 0; i < 105; i++) {
|
for (int i = 0; i < 105; i++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
return;
|
||||||
*val = i;
|
*val = i;
|
||||||
array_list_add(list, val);
|
array_list_add(list, val);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ static void test_data_compress_decompress_roundtrip() {
|
|||||||
size_t len = strlen(original);
|
size_t len = strlen(original);
|
||||||
|
|
||||||
char* buf = malloc(len);
|
char* buf = malloc(len);
|
||||||
|
if (!buf)
|
||||||
|
return;
|
||||||
memcpy(buf, original, len);
|
memcpy(buf, original, len);
|
||||||
Data* original_data = data_create(buf, len);
|
Data* original_data = data_create(buf, len);
|
||||||
EXPECT_NOT_NULL(original_data);
|
EXPECT_NOT_NULL(original_data);
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
static Data* random_data(int min_size, int max_size) {
|
static Data* random_data(int min_size, int max_size) {
|
||||||
int size = min_size + rand() % (max_size - min_size + 1);
|
int size = min_size + rand() % (max_size - min_size + 1);
|
||||||
char* buf = malloc(size);
|
char* buf = malloc(size);
|
||||||
|
if (!buf)
|
||||||
|
return NULL;
|
||||||
for (int i = 0; i < size; i++)
|
for (int i = 0; i < size; i++)
|
||||||
buf[i] = (char)(rand() % 256);
|
buf[i] = (char)(rand() % 256);
|
||||||
return data_create(buf, size);
|
return data_create(buf, size);
|
||||||
@@ -23,10 +25,16 @@ static void test_property_compress_roundtrip() {
|
|||||||
for (int iter = 0; iter < 10; iter++) {
|
for (int iter = 0; iter < 10; iter++) {
|
||||||
Data* original = random_data(1, 10000);
|
Data* original = random_data(1, 10000);
|
||||||
EXPECT_NOT_NULL(original);
|
EXPECT_NOT_NULL(original);
|
||||||
|
if (!original)
|
||||||
|
return;
|
||||||
|
|
||||||
size_t orig_size = original->size;
|
size_t orig_size = original->size;
|
||||||
void* orig_copy = malloc(orig_size);
|
void* orig_copy = malloc(orig_size);
|
||||||
EXPECT_NOT_NULL(orig_copy);
|
EXPECT_NOT_NULL(orig_copy);
|
||||||
|
if (!orig_copy) {
|
||||||
|
data_destroy(original);
|
||||||
|
return;
|
||||||
|
}
|
||||||
memcpy(orig_copy, original->data, orig_size);
|
memcpy(orig_copy, original->data, orig_size);
|
||||||
|
|
||||||
Data* compressed = data_compress(original, 3);
|
Data* compressed = data_compress(original, 3);
|
||||||
@@ -81,6 +89,8 @@ static void test_property_chunk_roundtrip() {
|
|||||||
|
|
||||||
int content_len = 1 + rand() % 4096;
|
int content_len = 1 + rand() % 4096;
|
||||||
char* content = malloc(content_len);
|
char* content = malloc(content_len);
|
||||||
|
if (!content)
|
||||||
|
return;
|
||||||
for (int i = 0; i < content_len; i++)
|
for (int i = 0; i < content_len; i++)
|
||||||
content[i] = (char)(rand() % 256);
|
content[i] = (char)(rand() % 256);
|
||||||
|
|
||||||
|
|||||||
@@ -119,9 +119,13 @@ static void test_queue_destroyer() {
|
|||||||
destroyer_calls = 0;
|
destroyer_calls = 0;
|
||||||
Queue* q = queue_create(5, my_destroyer);
|
Queue* q = queue_create(5, my_destroyer);
|
||||||
EXPECT_NOT_NULL(q);
|
EXPECT_NOT_NULL(q);
|
||||||
|
if (!q)
|
||||||
|
return;
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
break;
|
||||||
*val = i;
|
*val = i;
|
||||||
queue_enqueue(q, val);
|
queue_enqueue(q, val);
|
||||||
}
|
}
|
||||||
@@ -181,6 +185,8 @@ static void test_queue_multithreaded() {
|
|||||||
|
|
||||||
for (int i = 1; i <= 100; i++) {
|
for (int i = 1; i <= 100; i++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
break;
|
||||||
*val = i;
|
*val = i;
|
||||||
queue_enqueue_multithreaded(q, val, &mutex, &cnd_empty, &cnd_full);
|
queue_enqueue_multithreaded(q, val, &mutex, &cnd_empty, &cnd_full);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ static int mpmc_producer_func(void* arg) {
|
|||||||
ProducerCtx* ctx = (ProducerCtx*)arg;
|
ProducerCtx* ctx = (ProducerCtx*)arg;
|
||||||
for (int i = 1; i <= ITEMS_PER_PRODUCER; i++) {
|
for (int i = 1; i <= ITEMS_PER_PRODUCER; i++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
return thrd_error;
|
||||||
*val = ctx->producer_id * ITEMS_PER_PRODUCER + i;
|
*val = ctx->producer_id * ITEMS_PER_PRODUCER + i;
|
||||||
queue_enqueue_multithreaded(ctx->q, val, ctx->mutex, ctx->cnd_empty, ctx->cnd_full);
|
queue_enqueue_multithreaded(ctx->q, val, ctx->mutex, ctx->cnd_empty, ctx->cnd_full);
|
||||||
}
|
}
|
||||||
@@ -121,6 +123,8 @@ static int bp_producer_func(void* arg) {
|
|||||||
BackpressureCtx* ctx = (BackpressureCtx*)arg;
|
BackpressureCtx* ctx = (BackpressureCtx*)arg;
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
return thrd_error;
|
||||||
*val = i + 1;
|
*val = i + 1;
|
||||||
queue_enqueue_multithreaded(ctx->q, val, ctx->mutex, ctx->cnd_empty, ctx->cnd_full);
|
queue_enqueue_multithreaded(ctx->q, val, ctx->mutex, ctx->cnd_empty, ctx->cnd_full);
|
||||||
ctx->items_sent++;
|
ctx->items_sent++;
|
||||||
@@ -191,9 +195,13 @@ static void test_queue_rapid_create_destroy() {
|
|||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
Queue* q = queue_create(4, free);
|
Queue* q = queue_create(4, free);
|
||||||
EXPECT_NOT_NULL(q);
|
EXPECT_NOT_NULL(q);
|
||||||
|
if (!q)
|
||||||
|
return;
|
||||||
|
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
int* val = malloc(sizeof(int));
|
int* val = malloc(sizeof(int));
|
||||||
|
if (!val)
|
||||||
|
break;
|
||||||
*val = j;
|
*val = j;
|
||||||
queue_enqueue(q, val);
|
queue_enqueue(q, val);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user