Skip to content

Commit 5d7baeb

Browse files
committed
securityfs directory creation test
1 parent 965e324 commit 5d7baeb

File tree

3 files changed

+58
-190
lines changed

3 files changed

+58
-190
lines changed

home_hack/home_hack.c

-190
Original file line numberDiff line numberDiff line change
@@ -210,196 +210,6 @@ DECLARE_FUNC(chdir, char*path)
210210
211211
*/
212212

213-
#include <linux/sockios.h>
214-
#include <linux/if.h>
215-
216-
asmlinkage long sys_ioctl_wrapper(unsigned int fd, unsigned int cmd,
217-
unsigned long arg)
218-
{
219-
long ret;
220-
struct ifreq ifr;
221-
int i;
222-
if (current->hp_node <= 0) {
223-
return original_sys_ioctl(fd, cmd, arg);
224-
}
225-
226-
ret = original_sys_ioctl(fd, cmd, arg);
227-
/*
228-
if (cmd == SIOCGIFCONF) {
229-
230-
if (copy_from_user(&ifr, (struct ifconf __user *)arg, sizeof(ifr))) {
231-
return -EFAULT;
232-
}
233-
}
234-
*/
235-
if (cmd == SIOCGIFADDR) {
236-
237-
if (copy_from_user(&ifr, (struct ifreq __user *)arg, sizeof(ifr))) {
238-
return -EFAULT;
239-
}
240-
printk("***ioctl for fd: %d cmd SIOCGIFADDR\n", fd);
241-
242-
printk("*** ifr.ifr_ifrn.ifrn_name : %s\n",
243-
ifr.ifr_ifrn.ifrn_name);
244-
245-
// printk("*** %s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
246-
printk("***Addr ");
247-
for (i=0; i<14; ++i) {
248-
printk("%02x.", (ifr.ifr_ifru.ifru_addr.sa_data[i]) & 0xff);
249-
}
250-
printk("\n");
251-
ifr.ifr_ifru.ifru_addr.sa_data[5] = (char)(current->hp_node & 0xff);
252-
printk("new 5th : %02x.\n", (ifr.ifr_ifru.ifru_addr.sa_data[5]));
253-
254-
if (copy_to_user((struct ifreq __user *)arg, &ifr, sizeof(ifr))) {
255-
return -EFAULT;
256-
}
257-
258-
} else {
259-
// printk("***ioctl for fd: %d cmd %d\n", fd, cmd);
260-
}
261-
262-
return ret;
263-
}
264-
265-
266-
267-
asmlinkage long sys_unlink_wrapper(char *path)
268-
{
269-
long ret;
270-
char *new_path;
271-
272-
if (current->hp_node <= 0) {
273-
return original_sys_unlink(path);
274-
}
275-
276-
new_path = replace_path_if_necessary(path);
277-
if (new_path == NULL) {
278-
return original_sys_unlink(path);
279-
}
280-
printk("*** unlinking file %s by %d on %ld %s: \n", path, current->pid,
281-
current->hp_node, current->comm);
282-
283-
284-
ret = original_sys_unlink(new_path);
285-
printk("*** ret: %ld\n", ret);
286-
restore_path(path);
287-
return ret;
288-
}
289-
290-
291-
asmlinkage int sys_chdir_wrapper(/* const */ char *path)
292-
{
293-
int ret;
294-
char *new_path;
295-
296-
if (current->hp_node <= 0) {
297-
/*
298-
When the current process is not our target
299-
*/
300-
return original_sys_chdir(path);
301-
}
302-
new_path = replace_path_if_necessary(path);
303-
if (new_path == NULL) {
304-
return original_sys_chdir(path);
305-
}
306-
/*
307-
* Call the original sys_open - otherwise, we lose
308-
* the ability to open files
309-
*/
310-
ret = original_sys_chdir(new_path);
311-
restore_path(path);
312-
return ret;
313-
}
314-
315-
316-
317-
asmlinkage int sys_open_wrapper(/* const */ char *path, int flags, int mode)
318-
{
319-
int ret;
320-
char *new_path;
321-
if (current->hp_node <= 0) {
322-
return original_sys_open(path, flags, mode);
323-
}
324-
/*
325-
printk("*** Opened file by %d on %d %s: %s\n", current->pid,
326-
current->hp_node, current->comm, path);
327-
*/
328-
new_path = replace_path_if_necessary(path);
329-
if (new_path == NULL) {
330-
return original_sys_open(path, flags, mode);
331-
}
332-
/*
333-
* Call the original sys_open - otherwise, we lose
334-
* the ability to open files
335-
*/
336-
ret = original_sys_open(new_path, flags, mode);
337-
restore_path(path);
338-
return ret;
339-
}
340-
341-
asmlinkage long sys_lstat64_wrapper(char *path, struct stat64 *buf)
342-
{
343-
long ret;
344-
char *new_path;
345-
if (current->hp_node <= 0) {
346-
return original_sys_lstat64(path, buf);
347-
}
348-
349-
printk("*** Lstat64ed file %s by %d on %ld %s: \n", path, current->pid,
350-
current->hp_node, current->comm);
351-
352-
new_path = replace_path_if_necessary(path);
353-
if (new_path == NULL) {
354-
return original_sys_lstat64(path, buf);
355-
}
356-
ret = original_sys_lstat64(new_path, buf);
357-
printk("*** replaced: %s\n", new_path);
358-
printk("*** return val: %ld\n", ret);
359-
restore_path(path);
360-
return ret;
361-
}
362-
363-
asmlinkage long sys_stat_wrapper(char *path, struct __old_kernel_stat *buf)
364-
{
365-
long ret;
366-
char *new_path;
367-
if (current->hp_node <= 0) {
368-
return original_sys_stat(path, buf);
369-
}
370-
371-
printk("*** Stated file %s by %d on %ld %s: \n", path, current->pid,
372-
current->hp_node, current->comm);
373-
374-
new_path = replace_path_if_necessary(path);
375-
if (new_path == NULL) {
376-
return original_sys_stat(path, buf);
377-
}
378-
ret = original_sys_stat(new_path, buf);
379-
restore_path(path);
380-
return ret;
381-
}
382-
383-
asmlinkage long sys_stat64_wrapper(char *path, struct stat64 *buf)
384-
{
385-
long ret;
386-
char *new_path;
387-
if (current->hp_node <= 0) {
388-
return original_sys_stat64(path, buf);
389-
}
390-
391-
printk("*** Stat64ed file %s by %d on %ld %s: \n", path, current->pid,
392-
current->hp_node, current->comm);
393-
394-
new_path = replace_path_if_necessary(path);
395-
if (new_path == NULL) {
396-
return original_sys_stat64(path, buf);
397-
}
398-
ret = original_sys_stat64(new_path, buf);
399-
restore_path(path);
400-
return ret;
401-
}
402-
403213
/*
404214
Create functions that replaces sys_call_table entries.
405215
*/

secfs/Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
TARGET=secfs
2+
obj-m += $(TARGET).o
3+
4+
all:
5+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
6+
7+
clean:
8+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
9+
10+
check-syntax:
11+
LANG=C make -C /lib/modules/$(shell uname -r)/build M=/home/suzuki/kernel-study/$(TARGET) modules
12+
13+
install:
14+
sudo insmod $(TARGET).ko
15+
16+
uninstall:
17+
sudo rmmod $(TARGET)

secfs/secfs.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Security file system.
3+
*
4+
*/
5+
6+
7+
#include <linux/kernel.h>
8+
#include <linux/module.h>
9+
#include <linux/proc_fs.h>
10+
#include <linux/security.h>
11+
#include <linux/seq_file.h>
12+
#include <asm/uaccess.h>
13+
14+
15+
#define DIR_NAME "hp"
16+
17+
MODULE_AUTHOR("SUZUKI Tomohiro");
18+
MODULE_LICENSE("GPL");
19+
20+
struct dentry * hp_dir_entry;
21+
22+
int init_module(void)
23+
{
24+
hp_dir_entry = securityfs_create_dir(DIR_NAME, NULL);
25+
if (!hp_dir_entry) {
26+
printk(KERN_ALERT "failed securityfs_create_dir.\n");
27+
}
28+
if ((int)hp_dir_entry == -ENODEV) {
29+
printk(KERN_ALERT "securityfs is not enabled in this machine.\n");
30+
hp_dir_entry = NULL;
31+
}
32+
return 0;
33+
}
34+
35+
void cleanup_module(void)
36+
{
37+
if (hp_dir_entry) {
38+
securityfs_remove(hp_dir_entry);
39+
}
40+
}
41+

0 commit comments

Comments
 (0)