【问题标题】:How to read linux file permission programmatically in linux kernel如何在linux内核中以编程方式读取linux文件权限
【发布时间】:2019-03-08 12:04:25
【问题描述】:

如果我在linux内核中将权限声明为umode_t类型的变量模式,如何检查它是否具有读取或写入权限

例如 - 我将权限存储到 umode_t file_mode 中,现在如何在 linux 中以编程方式检查它是否具有读写权限

我尝试使用 filp->f_op->read,但即使文件具有读取权限,它也总是给我一个错误

umode_t input_file_mode;
filp = filp_open( args->inputfile,O_RDONLY,0 );
input_file_mode = filp->f_inode->i_mode;
if (!filp->f_op->read)
{
     error = -EACCES;
     printk("reading input file failed\n");
}

【问题讨论】:

  • 请包含minimal reproducible example,或至少包含一些实际代码。到目前为止,您所显示的内容没有用。
  • 添加它总是返回错误
  • 你在测试read函数是否为null。
  • 如何在内核中进行文件操作的简单答案是“不要”
  • 是的,如果它为空,我想返回一个错误,但即使启用了读取操作,它也会抛出一个错误

标签: c linux permissions kernel


【解决方案1】:

要检查用户是否对给定的 inode 具有特定权限,请使用 inode_permissions 函数。它在linux/fs.h 中声明并具有以下定义(在fs/namei.c 中):

/**
 * inode_permission - Check for access rights to a given inode
 * @inode: Inode to check permission on
 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
 *
 * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
 * this, letting us set arbitrary permissions for filesystem access without
 * changing the "normal" UIDs which are used for other things.
 *
 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
 */
int inode_permission(struct inode *inode, int mask)

使用示例:

if(inode_permission(inode, MAY_WRITE | MAY_READ) == 0) {
    // Current user has permissions to read and write the file.
}

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多