Sysfs文件系统read流程安全性分析

1 概述

随着对安全的逐渐深入的学习,Linux系统是无法绕过的坎,接下来就渐渐的学习Linux的各种文件系统安全性,同时结合源码进行跟读学习。

在查看本文时,需要了解如何创建一个sys文件,了解show和store函数

2 参考文章

Linux源码:

http://lxr.free-electrons.com/ 

Cndn参考博客:

http://blog.csdn.net/angle_birds/article/details/8315298

http://blog.csdn.net/dndxhej/article/details/7435634

http://blog.csdn.net/zclongembedded/article/details/8689099

http://www.cnblogs.com/huxiao-tee/p/4657851.html

http://www.cnblogs.com/armlinux/archive/2010/10/10/2396903.html

3 Sysfs的变迁史

Sysfs是在2.6版本新加入的文件系统,以前是不存在的,本文的代码主要是采用当前linux嵌入式设备常用的3.10和最新的4.6系统作对比。



4 linux源码跟读

4.1公共部分用户态到VFS调用

在进行read函数时,其实就是执行普通的read操作,这里直接跳过,查看通过gilbc等调用的SYSCALL函数。

文件路径:fs/read_write.c 查找SYSCALL关键字

SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
{
         struct fd f = fdget(fd);
         ssize_t ret = -EBADF;
 
         if (f.file) {
                 loff_t pos = file_pos_read(f.file);
                 ret = vfs_read(f.file, buf, count, &pos);//接下来调用的是vfs_read
                 file_pos_write(f.file, pos);
                 fdput(f);
         }
         return ret;
}
文件路径:fs/read_write.c 查找vfs_read关键字
size_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
        ssize_t ret;

        if (!(file->f_mode & FMODE_READ))
                return -EBADF;
        if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
                return -EINVAL;
        if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
                return -EFAULT;

        ret = rw_verify_area(READ, file, pos, count);
        if (ret >= 0) {
                count = ret;
                if (file->f_op->read)
                        ret = file->f_op->read(file, buf, count, pos);//从这一步开始就调用到了file_operations中的函数
                else
                        ret = do_sync_read(file, buf, count, pos);
                if (ret > 0) {
                        fsnotify_access(file);
                        add_rchar(current, ret);
                }
                inc_syscr(current);
        }

        return ret;
}

4.2 linux 3.10中Sysfs的实现

文件路径:fs/sysfs/file.c 


                
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值