LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 917|回复: 7

如何调用LSM中的函数,请帮我分析例子中的错误

[复制链接]
发表于 2005-10-25 16:58:40 | 显示全部楼层 |阅读模式
我在2.6内核上写了一个程序,只是为了试验我能否调用LSM函数:
/*******************/
#include <linux/security.h>

int main ()
{
    return 0;
}

/******************/
存为t.c
用 gcc t.c -o t
编译出错 linux/security.h: No such file or directory
why, 不是说2.6内核已经包含LSM了吗?
发表于 2005-10-25 17:23:54 | 显示全部楼层
你是不是没有安装Linux的头文件?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-25 19:07:04 | 显示全部楼层
我安装的是Red Hat Enterprise Linux, 所有的开发组件都安装了,还需要另外安装头文件吗,怎么安装?
回复 支持 反对

使用道具 举报

发表于 2005-10-25 19:12:01 | 显示全部楼层
Post by lora
我在2.6内核上写了一个程序,只是为了试验我能否调用LSM函数:
/*******************/
#include <linux/security.h>

int main ()
{
    return 0;
}

/******************/
存为t.c
用 gcc t.c -o t
编译出错 linux/security.h: No such file or directory
why, 不是说2.6内核已经包含LSM了吗?


试试这个目录: /lib/module/2.***/build/include/***
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-25 19:20:12 | 显示全部楼层
rickxbx, thanks.
你写过调用LSM函数的简单例子吗能不能贴一下
回复 支持 反对

使用道具 举报

发表于 2005-10-25 22:25:05 | 显示全部楼层
Post by lora
rickxbx, thanks.
你写过调用LSM函数的简单例子吗能不能贴一下


呵呵,我基本上是一个"理论工作者",因为没有需求,所以不曾写过
回复 支持 反对

使用道具 举报

发表于 2005-10-26 10:25:19 | 显示全部楼层
这是内核中提供的函数吧,你应该写一个module来调啊,用户空间的程序怎么调啊?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-26 16:08:51 | 显示全部楼层
Thanks, 问题已解决。方法如下:
程序myLSM.c(我是从lids_lsm.c修改而来,省略部分请参考LIDS项目中该文件,我的操作系统是Red Hat Enterprise Linux,内核是2.6.9)
/****************************/
#ifndef __KERNEL__
        #define __KERNEL__
#endif
#ifndef MODULE
        #define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include </lib/modules/2.6.9-11.EL/source/include/linux/security.h>
#include <linux/netfilter.h>
#include <linux/netlink.h>

static int lids_ptrace (struct task_struct *parent, struct task_struct *child)
{
        return 0;
}
...............
static int lids_unregister (const char *name, struct security_operations *ops)
{
        return -EINVAL;
}

struct security_operations lids_security_ops = {
        ptrace:                        lids_ptrace,
        capget:                        lids_capget,
        capset_check:                lids_capset_check,
        capset_set:                lids_capset_set,
        acct:                        lids_acct,
        sysctl:                        lids_sysctl,
        capable:                lids_capable,
        quotactl:                lids_quotactl,
        quota_on:                lids_quota_on,
        syslog:                        lids_syslog,
        vm_enough_memory:                NULL,
       
        bprm_alloc_security:        lids_binprm_alloc_security,
        bprm_free_security:        lids_binprm_free_security,
        bprm_apply_creds:                NULL,
        bprm_set_security:                lids_binprm_set_security,
        bprm_check_security:                lids_binprm_check_security,
        bprm_secureexec:                NULL,

        sb_alloc_security:        lids_sb_alloc_security,
        sb_free_security:        lids_sb_free_security,
        sb_copy_data:        NULL,
        sb_kern_mount:        NULL,
        sb_statfs:                lids_sb_statfs,
        sb_mount:                lids_mount,
        sb_check_sb:        lids_check_sb,
        sb_umount:                lids_umount,
        sb_umount_close:        lids_umount_close,
        sb_umount_busy:        lids_umount_busy,
        sb_post_remount:        lids_post_remount,
        sb_post_mountroot:        lids_post_mountroot,
        sb_post_addmount:        lids_post_addmount,
        sb_pivotroot:        NULL,
        sb_post_pivotroot:        NULL,

        inode_alloc_security:        lids_inode_alloc_security,
        inode_free_security:        lids_inode_free_security,
        inode_create:                lids_inode_create,
        inode_post_create:        lids_inode_post_create,
        inode_link:                lids_inode_link,
        inode_post_link:        lids_inode_post_link,
        inode_unlink:                lids_inode_unlink,
        inode_symlink:        lids_inode_symlink,
        inode_post_symlink:        lids_inode_post_symlink,
        inode_mkdir:                lids_inode_mkdir,
        inode_post_mkdir:        lids_inode_post_mkdir,
        inode_rmdir:                lids_inode_rmdir,
        inode_mknod:                lids_inode_mknod,
        inode_post_mknod:        lids_inode_post_mknod,
        inode_rename:                lids_inode_rename,
        inode_post_rename:        lids_inode_post_rename,
        inode_readlink:        lids_inode_readlink,
        inode_follow_link:        lids_inode_follow_link,
        inode_permission:        lids_inode_permission,
        inode_setattr:        lids_inode_setattr,
        inode_getattr:        NULL,
        inode_delete:        lids_delete,
        inode_setxattr:        lids_inode_setxattr,
        inode_post_setxattr:        NULL,
        inode_getxattr:        lids_inode_getxattr,
        inode_listxattr:        lids_inode_listxattr,
        inode_removexattr:        lids_inode_removexattr,
        inode_getsecurity:        NULL,
        inode_setsecurity:        NULL,
        inode_listsecurity:        NULL,
       
        file_permission:        lids_file_permission,
        file_alloc_security:        lids_file_alloc_security,
        file_free_security:        lids_file_free_security,
        file_ioctl:                lids_file_ioctl,
        file_mmap:                lids_file_mmap,
        file_mprotect:        lids_file_mprotect,
        file_lock:                lids_file_lock,
        file_fcntl:                lids_file_fcntl,
        file_set_fowner:        lids_file_set_fowner,
        file_send_sigiotask:        lids_file_send_sigiotask,
        file_receive:        lids_file_receive,
       
        task_create:                lids_task_create,
        task_alloc_security:        lids_task_alloc_security,
        task_free_security:        NULL,
        task_setuid:        lids_task_setuid,
        task_post_setuid:        lids_task_post_setuid,
        task_setgid:                lids_task_setgid,
        task_setpgid:        lids_task_setpgid,
        task_getpgid:        lids_task_getpgid,
        task_getsid:                lids_task_getsid,
        task_setgroups:        lids_task_setgroups,
        task_setnice:        lids_task_setnice,
        task_setrlimit:        lids_task_setrlimit,
        task_setscheduler:        lids_task_setscheduler,
        task_getscheduler:        lids_task_getscheduler,
        task_kill:                lids_task_kill,       
        task_wait:                lids_task_wait,
        task_prctl:                lids_task_prctl,
        task_reparent_to_init:        NULL,
        task_to_inode:        NULL,

        ipc_permission:        lids_ipc_permission,
       
        msg_msg_alloc_security:        lids_msg_msg_alloc_security,
        msg_msg_free_security:        lids_msg_msg_free_security,
       
        msg_queue_alloc_security:        lids_msg_queue_alloc_security,
        msg_queue_free_security:        lids_msg_queue_free_security,
        msg_queue_associate:        lids_msg_queue_associate,
        msg_queue_msgctl:                lids_msg_queue_msgctl,
        msg_queue_msgsnd:                lids_msg_queue_msgsnd,
        msg_queue_msgrcv:                lids_msg_queue_msgrcv,

        shm_alloc_security:        lids_shm_alloc_security,
        shm_free_security:        lids_shm_free_security,
        shm_associate:        lids_shm_associate,
        shm_shmctl:                lids_shm_shmctl,
        shm_shmat:                lids_shm_shmat,       
       
        sem_alloc_security:        lids_sem_alloc_security,
        sem_free_security:        lids_sem_free_security,
        sem_associate:        lids_sem_associate,
        sem_semctl:        lids_sem_semctl,
        sem_semop:        lids_sem_semop,

        netlink_send:                lids_netlink_send,
        netlink_recv:                lids_netlink_recv,

        register_security:        lids_register,
        unregister_security:        lids_unregister,
       
        d_instantiate:        NULL,
       
        getprocattr:                NULL,
        setprocattr:                NULL,
       
        unix_stream_connect:        lids_socket_unix_stream_connect,
        unix_may_send:        lids_socket_unix_may_send,

        socket_create:                        lids_socket_create,
        socket_post_create:                lids_socket_post_create,
        socket_bind:                        lids_socket_bind,
        socket_connect:                lids_socket_connect,
        socket_listen:                        lids_socket_listen,
        socket_accept:                        lids_socket_accept,
        socket_post_accept:        NULL,
        socket_sendmsg:                lids_socket_sendmsg,
        socket_recvmsg:                lids_socket_recvmsg,
        socket_getsockname:                lids_socket_getsockname,
        socket_getpeername:                lids_socket_getpeername,
        socket_getsockopt:                lids_socket_getsockopt,
        socket_setsockopt:                lids_socket_setsockopt,
        socket_shutdown:                lids_socket_shutdown,
        socket_sock_rcv_skb:                lids_sock_rcv_skb,
        socket_getpeersec:                NULL,
        sk_alloc_security:                NULL,
        sk_free_security:                NULL,
};

extern void setup_lids_module(void);

static int __init lids_lsm_init (void)
{
                /* register ourselves with the security framework */
        printk ("LSM init\n");
        if (register_security (&lids_security_ops)) {
                printk (KERN_INFO "Failure registering LIDS with the kernel\n");
                return -EINVAL;
        }


#ifdef MODULE
        printk(KERN_NOTICE "Setting up LIDS...\n");

        printk(KERN_NOTICE "Finished setting up LIDS.\n");
#endif
        printk(KERN_INFO "Linux Intrusion Detection System initialized\n");
        return 0;
}

static void __exit lids_lsm_exit (void)
{
        if (unregister_security (&lids_security_ops)) {
                printk(KERN_INFO "Failure unregistering LIDS with the kernel\n");
        }

        printk(KERN_INFO "Linux Intrusion Detection System Quit\n");
}

module_init (lids_lsm_init);
module_exit (lids_lsm_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("myLSM");

/********************************/

MakeFile文件如下:
/********************************/
obj-m := myLSM.o
KERNELBUILD := /lib/modules/`uname -r`/build
default:
        make -C $(KERNELBUILD) M=$(shell pwd) modules
clean:
        rm -rf *.o .*.cmd *.ko *.mod.c .tmp_versions
/********************************/
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表