我的目标是在内核模块中编写一个允许获取 root 访问权限的函数。在最初的时候我有这个:
struct task_struct *cur_task;
struct cred *credz;
/*obtain root access*/
cur_task=current;
credz=cur_task->cred;
credz->uid=0;
credz->gid=0;
credz->suid=0;
credz->sgid=0;
credz->euid=0;
credz->egid=0;
它有效,但我尝试删除有关 const 变量的警告。所以我尝试使用 memcopy 绕过它。但我有一个内核恐慌。
我认为我的错误是内存分配(kmem 缓存)
static struct kmem_cache *cred_jar; //global
char func(void){
struct task_struct *cur_task;
const struct cred *old;
struct cred *credz;
cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
credz = kmem_cache_alloc(cred_jar, GFP_KERNEL);
if (!credz){
return 0;
}
/* obtain root access in shell*/
cur_task=current;
/**/
old = cur_task->cred;
/* remove warning const */
memcpy(credz, old, sizeof(struct cred));
credz->uid=0;
credz->gid=0;
credz->suid=0;
credz->sgid=0;
credz->euid=0;
credz->egid=0;
cur_task->cred=credz;
kfree(old);
}
如果您有任何想法来纠正它,我很感兴趣。