diff --git a/shufflecake-userland/Makefile.sources b/shufflecake-userland/Makefile.sources index d2313c8..b34553d 100644 --- a/shufflecake-userland/Makefile.sources +++ b/shufflecake-userland/Makefile.sources @@ -27,7 +27,7 @@ #### Main files #### -PROJ_SRCS := $(addprefix utils/,crypto.c disk.c dm.c file.c string.c input.c) +PROJ_SRCS := $(addprefix utils/,crypto.c disk.c dm.c file.c string.c input.c kernmod.c) PROJ_SRCS += $(addprefix header/,position_map_legacy.c position_map_lite.c volume_master_block_legacy.c volume_master_block_lite.c device_master_block.c) PROJ_SRCS += $(addprefix operations/,volume_header_legacy.c volume_header_lite.c devmapper_legacy.c devmapper_lite.c dmb.c) PROJ_SRCS += $(addprefix commands/,init_legacy.c init_lite.c open_legacy.c open_lite.c close.c test_pwd.c change_pwd.c) diff --git a/shufflecake-userland/include/utils/kernmod.h b/shufflecake-userland/include/utils/kernmod.h new file mode 100644 index 0000000..a5c42a9 --- /dev/null +++ b/shufflecake-userland/include/utils/kernmod.h @@ -0,0 +1,42 @@ +/* + * Copyright The Shufflecake Project Authors (2022) + * Copyright The Shufflecake Project Contributors (2022) + * Copyright Contributors to the The Shufflecake Project. + * + * See the AUTHORS file at the top-level directory of this distribution and at + * + * + * This file is part of the program shufflecake-c, which is part of the + * Shufflecake Project. Shufflecake is a plausible deniability (hidden storage) + * layer for Linux. See . + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 2 of the License, or (at your option) + * any later version. This program is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the + * GNU General Public License along with this program. + * If not, see . + */ + +#ifndef _UTILS_KERNMOD_H_ +#define _UTILS_KERNMOD_H_ + + +/***************************************************** + * INCLUDE SECTION * + *****************************************************/ + +#include + + +/***************************************************** + * PUBLIC FUNCTIONS PROTOTYPES * + *****************************************************/ + +bool sflc_isKernelModuleLoaded(char *module_name); + + +#endif /* _UTILS_KERNOMD_H_ */ diff --git a/shufflecake-userland/include/utils/sflc.h b/shufflecake-userland/include/utils/sflc.h index f97fde2..6ab11b4 100644 --- a/shufflecake-userland/include/utils/sflc.h +++ b/shufflecake-userland/include/utils/sflc.h @@ -40,6 +40,8 @@ /* Name of the DM target in the kernel */ #define SFLC_DM_TARGET_NAME "shufflecake" +/* Name of the kernel module once inserted */ +#define SFLC_KERNMOD_NAME "dm_sflc" /* Modes */ #define SFLC_MODE_LEGACY 0 diff --git a/shufflecake-userland/src/cli/dispatch.c b/shufflecake-userland/src/cli/dispatch.c index d28e01f..24a002e 100644 --- a/shufflecake-userland/src/cli/dispatch.c +++ b/shufflecake-userland/src/cli/dispatch.c @@ -35,6 +35,7 @@ #include "cli.h" #include "utils/sflc.h" #include "utils/disk.h" +#include "utils/kernmod.h" #include "utils/log.h" @@ -185,7 +186,11 @@ int sflc_cli_dispatch(int argc, char **argv) { return EINVAL; } - // TODO check that dm_sflc is loaded + /* Check that the kernel module is loaded */ + if (!sflc_isKernelModuleLoaded(SFLC_KERNMOD_NAME)) { + printf("Error: kernel module dm-sflc is not inserted.\n"); + return EINVAL; + } /* Check that input is actually a block device */ if (strncmp(args.block_device, "/dev/", 5) != 0 || !sflc_disk_isBlockDevice(args.block_device)) { printf("Error: '%s' is not a valid block device.\n", args.block_device); diff --git a/shufflecake-userland/src/utils/kernmod.c b/shufflecake-userland/src/utils/kernmod.c new file mode 100644 index 0000000..6ecde3d --- /dev/null +++ b/shufflecake-userland/src/utils/kernmod.c @@ -0,0 +1,65 @@ +/* + * Copyright The Shufflecake Project Authors (2022) + * Copyright The Shufflecake Project Contributors (2022) + * Copyright Contributors to the The Shufflecake Project. + * + * See the AUTHORS file at the top-level directory of this distribution and at + * + * + * This file is part of the program shufflecake-c, which is part of the + * Shufflecake Project. Shufflecake is a plausible deniability (hidden storage) + * layer for Linux. See . + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 2 of the License, or (at your option) + * any later version. This program is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the + * GNU General Public License along with this program. + * If not, see . + */ + +/***************************************************** + * INCLUDE SECTION * + *****************************************************/ + +#include +#include +#include + +#include "utils/kernmod.h" +#include "utils/sflc.h" +#include "utils/log.h" + + +/***************************************************** + * PUBLIC FUNCTIONS DEFINITIONS * + *****************************************************/ + +bool sflc_isKernelModuleLoaded(char *module_name) { + FILE *fp; + char line[SFLC_BIGBUFSIZE]; + + // Open /proc/modules to check loaded modules + fp = fopen("/proc/modules", "r"); + if (fp == NULL) { + sflc_log_error("Failed to open /proc/modules"); + return false; + } + + // Read each line in /proc/modules + while (fgets(line, sizeof(line), fp) != NULL) { + // Compare the module name in the current line with the input module name + char *current_module = strtok(line, " "); // First token in the line is the module name + if (current_module != NULL && strcmp(current_module, module_name) == 0) { + fclose(fp); + return true; // Module is loaded + } + } + + fclose(fp); + return false; // Module not found +} +