From f2b53c4cb1316bfd11068b4df1c6f25aa1133e53 Mon Sep 17 00:00:00 2001 From: dianshi Date: Mon, 22 Jun 2020 21:50:17 +0100 Subject: First version that can mount some actual fs --- Makefile | 3 +- README.txt | 4 ++ nmount.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 203 insertions(+), 38 deletions(-) create mode 100644 README.txt diff --git a/Makefile b/Makefile index 217284f..5f8c38a 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,3 @@ make: - gcc nmount.c -o nmount \ No newline at end of file + gcc -Wall nmount.c -o nmount + gcc -static nmount.c -o nmount_static \ No newline at end of file diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..e5ed452 --- /dev/null +++ b/README.txt @@ -0,0 +1,4 @@ + +nmount -t proc -d /proc2 -f nosuid,nodev,noexec +nmount -v -t proc -d /Process -f nosuid,nodev,noexec +nmount -v -t ext4 -s /dev/mmcblk0p7 -d /mnt/disk0 -f nodev,noexec,nosuid \ No newline at end of file diff --git a/nmount.c b/nmount.c index 8e51ec3..8142531 100644 --- a/nmount.c +++ b/nmount.c @@ -6,39 +6,47 @@ #include #include #include +#include +#include +#include +#include + ///////////////////////////////////////////////////////////////////////////////////////// /*FLAGS FROM KERNEL*/ -//#define MS_RDONLY 1 /* Mount read-only */ -//#define MS_NOSUID 2 /* Ignore suid and sgid bits */ -//#define MS_NODEV 4 /* Disallow access to device special files */ -//#define MS_NOEXEC 8 /* Disallow program execution */ -//#define MS_SYNCHRONOUS 16 /* Writes are synced at once */ -//#define MS_REMOUNT 32 /* Alter flags of a mounted FS */ -//#define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */ -//#define MS_DIRSYNC 128 /* Directory modifications are synchronous */ -//#define MS_NOATIME 1024 /* Do not update access times. */ -//#define MS_NODIRATIME 2048 /* Do not update directory access times */ -//#define MS_BIND 4096 -//#define MS_MOVE 8192 -//#define MS_REC 16384 -//#define MS_VERBOSE 32768 /* War is peace. Verbosity is silence. -// MS_VERBOSE is deprecated. */ -//#define MS_SILENT 32768 -//#define MS_POSIXACL (1<<16) /* VFS does not apply the umask */ -//#define MS_UNBINDABLE (1<<17) /* change to unbindable */ -//#define MS_PRIVATE (1<<18) /* change to private */ -//#define MS_SLAVE (1<<19) /* change to slave */ -//#define MS_SHARED (1<<20) /* change to shared */ -//#define MS_RELATIME (1<<21) /* Update atime relative to mtime/ctime. */ -//#define MS_KERNMOUNT (1<<22) /* this is a kern_mount call */ -//#define MS_I_VERSION (1<<23) /* Update inode I_version field */ -//#define MS_STRICTATIME (1<<24) /* Always perform atime updates */ -//#define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */ +#define _MS_RDONLY (1<<0) +#define _MS_NOSUID (1<<1) +#define _MS_NODEV (1<<2) +#define _MS_NOEXEC (1<<3) +#define _MS_SYNCHRONOUS (1<<4) +#define _MS_REMOUNT (1<<5) +#define _MS_MANDLOCK (1<<6) +#define _MS_DIRSYNC (1<<7) +#define _MS_NOATIME (1<<10) +#define _MS_NODIRATIME (1<<11) +#define _MS_BIND (1<<12) +#define _MS_MOVE (1<<13) +#define _MS_REC (1<<14) +#define _MS_SILENT (1<<15) +#define _MS_POSIXACL (1<<16) +#define _MS_UNBINDABLE (1<<17) +#define _MS_PRIVATE (1<<18) +#define _MS_SLAVE (1<<19) +#define _MS_SHARED (1<<20) +#define _MS_RELATIME (1<<21) +#define _MS_KERNMOUNT (1<<22) +#define _MS_I_VERSION (1<<23) +#define _MS_STRICTATIME (1<<24) +#define _MS_LAZYTIME (1<<25) #define STRUCT_LEN(VAR,STRUCT) ((sizeof(VAR)/sizeof(STRUCT))-1) +#define max(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) + typedef struct vfs_option_params { char *val; char *descr; @@ -60,16 +68,17 @@ typedef struct vfs_options { char *options; char *description; - vfs_option_params **params; + vfs_option_params *params; } vfs_options; vfs_options vfs_options_proc[] = { - {"hidepid=\%u" ,"", &vfs_options_proc_params_hidepid}, - {"gid=\%u" ,"user group that can access process", &vfs_options_proc_params_gid}, + {"hidepid=\%u" ,"", (struct vfs_option_params *)&vfs_options_proc_params_hidepid}, + {"gid=\%u" ,"user group that can access process", (struct vfs_option_params *)&vfs_options_proc_params_gid}, {NULL,NULL,NULL} }; + static const struct vfs { char *name; long flags; @@ -78,7 +87,7 @@ static const struct vfs { } vfstab[] = { {"devtmpfs", 0, "/Device", NULL}, {"mqueue", 0, "/MQueue/", NULL}, - {"proc", 0, "/Process", &vfs_options_proc}, + {"proc", 0, "/Process", (struct vfs_options*)&vfs_options_proc}, {"tmpfs", 0, "/Ram", NULL}, {"sysfs", 0, "/System", NULL}, {"ext4", 0, NULL, NULL}, @@ -86,11 +95,31 @@ static const struct vfs { {NULL, 0, NULL, NULL}, }; +static const struct mount_flags +{ + char *name; + unsigned long bitfield; +} mountflgs[] = { + {"bind",_MS_BIND}, + {"move",_MS_MOVE}, + {"rdonly",_MS_RDONLY}, + {"lazytime",_MS_LAZYTIME}, + {"nodev",_MS_NODEV}, + {"noexec",_MS_NOEXEC}, + {"nosuid",_MS_NOSUID}, + {"remount",_MS_REMOUNT}, + {"silent",_MS_SILENT}, + {"sync",_MS_SYNCHRONOUS}, + {NULL,0}, +}; + typedef struct { - char *f_device; + char *f_source_dir; + char *f_target_dir; char *f_type; char *f_dir; char *f_flags;//g_params.f_output = 1; + unsigned long mount_flags; char *f_options; int f_verbose; int f_helper; @@ -102,9 +131,9 @@ void helper(char *progname) { printf("Usage: %s [OPTS]\n\n" "Version: 0.0.1 \n" - "-d device to mount\n" + "-s source directory\n" + "-d target directory\n" "-t filesystem type\n" - "-d mount directory\n" "-f mount flags\n" "-o mount fs options\n" "-v verbose output\n" @@ -130,6 +159,33 @@ void sig_handler(int signo) exit(0); } +int check_filetype(char *fsname) +{ + int i; + for (i=0;i