ローダは、static LIST_HEAD(formats)のformatsヘッドにリストされ、ローダの検索はこのリストのlinux_binfmtの.load_binaryを成功するまで順次コールする事で実現してます。.load_binaryのマッチ条件さえ実装すれば、既存ローダの各種名称等を変更する事なく、独自のローダとしてを構築できます。(ローダの機能でなく、/procのように、ユーザ/カーネルのやり取りする目的での使用も可能です。)
サンプルはスクリプトでsuidを有効とするローダです。実態はカーネルソースroot/fs/binfmt_script.c(スクリプトローダ)その物で、#!のスクリプトコマンドを##とし、prepare_binprm()でスクリプトコマンドのbprmを設定した後、スクリプトファイルのcredで再更新しているだけです。
#include <linux/module.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/binfmts.h>
#include <linux/init.h>
#include <linux/file.h>
#include <linux/err.h>
#include <linux/fs.h>
static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
{
const char *i_arg, *i_name;
char *cp;
struct file *file;
char interp[BINPRM_BUF_SIZE];
int retval;
int euid;
// if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') ||
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '#') ||
(bprm->recursion_depth > BINPRM_MAX_RECURSION))
return -ENOEXEC;
bprm->recursion_depth++;
allow_write_access(bprm->file);
fput(bprm->file);
bprm->file = NULL;
bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
if ((cp = strchr(bprm->buf, '\n')) == NULL)
cp = bprm->buf+BINPRM_BUF_SIZE-1;
*cp = '\0';
while (cp > bprm->buf) {
cp--;
if ((*cp == ' ') || (*cp == '\t'))
*cp = '\0';
else
break;
}
for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
if (*cp == '\0')
return -ENOEXEC; /* No interpreter name found */
i_name = cp;
i_arg = NULL;
for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
while ((*cp == ' ') || (*cp == '\t'))
*cp++ = '\0';
if (*cp)
i_arg = cp;
strcpy (interp, i_name);
retval = remove_arg_zero(bprm);
if (retval)
return retval;
retval = copy_strings_kernel(1, &bprm->interp, bprm);
if (retval < 0) return retval;
bprm->argc++;
if (i_arg) {
retval = copy_strings_kernel(1, &i_arg, bprm);
if (retval < 0) return retval;
bprm->argc++;
}
retval = copy_strings_kernel(1, &i_name, bprm);
if (retval) return retval;
bprm->argc++;
bprm->interp = interp;
file = open_exec(interp);
if (IS_ERR(file))
return PTR_ERR(file);
bprm->file = file;
printk("uid:%d suid:%d euid:%d fsuid:%d\n",
bprm->cred->uid, bprm->cred->suid, bprm->cred->euid, bprm->cred->fsuid);
euid = bprm->cred->euid;
retval = prepare_binprm(bprm);
if (retval < 0)
return retval;
bprm->cred->uid = euid;
return search_binary_handler(bprm,regs);
}
static struct linux_binfmt script_format = {
.module = THIS_MODULE,
.load_binary = load_script,
};
static int __init init_script_binfmt(void)
{
return register_binfmt(&script_format);
}
static void __exit exit_script_binfmt(void)
{
unregister_binfmt(&script_format);
}
core_initcall(init_script_binfmt);
module_exit(exit_script_binfmt);
MODULE_LICENSE("GPL");
検証結果
[root@localhost lkm]# cat /home/kitamura/suid/hoge
12345
[kitamura@localhost suid]$ ls -l
:
-rw-r----- 1 root root 6 7月 5 07:20 hoge
-rwxrwxr-x 1 kitamura kitamura 19 7月 9 01:12 shell1.sh
-rwsrwxrwx 1 root kitamura 19 7月 8 11:27 shell2.sh
[kitamura@localhost suid]$ cat shell1.sh
#!/bin/sh
cat hoge
[kitamura@localhost suid]$ cat shell2.sh
##/bin/sh
cat hoge
[root@localhost lkm]# insmod load_suid.ko
[kitamura@localhost suid]$ ./shell1.sh
cat: hoge: 許可がありません
[kitamura@localhost suid]$ ./shell2.sh
12345
[kitamura@localhost suid]$ dmesg
:
[ 2113.072208] uid:1000 suid:0 euid:0 fsuid:0
補足
bprm->cred->fsuid = euidでは、suidは有効となりませんでした。bprm->cred->uidに権限を設定しないと効果ないようです。シェル起動のためload_elf()がコールされますが、その実装依存故という事かと思います。