gpt4 book ai didi

Linux 内核 alloc 缓存去除 const 警告

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:12 25 4
gpt4 key购买 nike

我的目标是在内核模块中编写一个允许获得 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 来绕过它。但是我遇到了内核 panic 。

我认为我的错误是内存分配(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);
}

如果您有任何改正的想法,我很感兴趣。

最佳答案

我认为它被标记为 const 的原因是您不应该更改或替换它,因此内核 panic

关于Linux 内核 alloc 缓存去除 const 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com