[英]'strsep' causing Linux kernel freeze
我在用户空间中有一个程序,它可以写入内核模块中的sysfs文件。 我已经隔离出崩溃的原因很可能就是这个特定的功能,因为当我在到达此点之前运行用户代码时,它不会崩溃,但是当我添加写入代码时,它很有可能崩溃。 我怀疑我解析字符串的方式会导致内存错误,但我不明白为什么。
我正在使用内核版本3.2和python 2.7
崩溃是指整个系统死机,我必须重新启动它或将VM恢复到以前的快照。
用户编写代码(python):
portFile = open(realDstPath, "w")
portFile.write(str(ipToint(srcIP)) + "|" + str(srcPort) + "|")
portFile.close()
内核代码:
ssize_t requestDstAddr( struct device *dev,
struct device_attribute *attr,
const char *buff,
size_t count)
{
char *token;
char *localBuff = kmalloc(sizeof(char) * count, GFP_ATOMIC);
long int temp;
if(localBuff == NULL)
{
printk(KERN_ERR "ERROR: kmalloc failed\n");
return -1;
}
memcpy(localBuff, buff, count);
spin_lock(&conntabLock);
//parse values passed from proxy
token = strsep(&localBuff, "|");
kstrtol(token, 10, &temp);
requestedSrcIP = htonl(temp);
token = strsep(&localBuff, "|");
kstrtol(token, 10, &temp);
requestedSrcPort = htons(temp);
spin_unlock(&conntabLock);
kfree(localBuff);
return count;
}
仔细观察strsep
。 从man strsep :
char *strsep(char **stringp, const char *delim);
... and *stringp is updated to point past the token. ...
在您的代码中,您可以执行以下操作:
char *localBuff = kmalloc(sizeof(char) * count, GFP_ATOMIC)
...
token = strsep(&localBuff, "|");
...
kfree(localBuff);
在strsep
调用后会更新localBuff
变量。 因此,调用kfree
的指针不相同。 这允许非常奇怪的行为。 使用临时指针保存strsep
函数的状态。 并检查它的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.