Linux社区关于链表的bug讨论我们要看一下

最近在Linux社区看到一个关于内核链表的讨论

原文讨论链接:

https://lwn.net/SubscriberLink/885941/01fdc39df2ecc25f/

fc0f6d87e3bc063ab0411c8580f61232.png

先用例子说明怎么使用内核链表

list.h

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef LIST_H
#define LIST_H/** Copied from include/linux/...*/#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)/*** container_of - cast a member of a structure out to the containing structure* @ptr: the pointer to the member.* @type: the type of the container struct this is embedded in.* @member: the name of the member within the struct.**/
#define container_of(ptr, type, member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)( (char *)__mptr - offsetof(type,member) );})struct list_head {struct list_head *next, *prev;
};#define LIST_HEAD_INIT(name) { &(name), &(name) }#define LIST_HEAD(name) \struct list_head name = LIST_HEAD_INIT(name)/*** list_entry - get the struct for this entry* @ptr: the &struct list_head pointer.* @type: the type of the struct this is embedded in.* @member: the name of the list_head within the struct.*/
#define list_entry(ptr, type, member) \container_of(ptr, type, member)/*** list_for_each_entry - iterate over list of given type* @pos: the type * to use as a loop cursor.* @head: the head for your list.* @member: the name of the list_head within the struct.*/
#define list_for_each_entry(pos, head, member) \for (pos = list_entry((head)->next, typeof(*pos), member); \&pos->member != (head); \pos = list_entry(pos->member.next, typeof(*pos), member))/*** list_for_each_entry_safe - iterate over list of given type safe against removal of list entry* @pos: the type * to use as a loop cursor.* @n: another type * to use as temporary storage* @head: the head for your list.* @member: the name of the list_head within the struct.*/
#define list_for_each_entry_safe(pos, n, head, member) \for (pos = list_entry((head)->next, typeof(*pos), member), \n = list_entry(pos->member.next, typeof(*pos), member); \&pos->member != (head); \pos = n, n = list_entry(n->member.next, typeof(*n), member))/*** list_empty - tests whether a list is empty* @head: the list to test.*/
static inline int list_empty(const struct list_head *head)
{return head->next == head;
}/** Insert a new entry between two known consecutive entries.** This is only for internal list manipulation where we know* the prev/next entries already!*/
static inline void __list_add(struct list_head *_new,struct list_head *prev,struct list_head *next)
{next->prev = _new;_new->next = next;_new->prev = prev;prev->next = _new;
}/*** list_add_tail - add a new entry* @new: new entry to be added* @head: list head to add it before** Insert a new entry before the specified head.* This is useful for implementing queues.*/
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{__list_add(_new, head->prev, head);
}/** Delete a list entry by making the prev/next entries* point to each other.** This is only for internal list manipulation where we know* the prev/next entries already!*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{next->prev = prev;prev->next = next;
}#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
/*** list_del - deletes entry from list.* @entry: the element to delete from the list.* Note: list_empty() on entry does not return true after this, the entry is* in an undefined state.*/
static inline void list_del(struct list_head *entry)
{__list_del(entry->prev, entry->next);entry->next = (struct list_head*)LIST_POISON1;entry->prev = (struct list_head*)LIST_POISON2;
}
#endif

test.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"struct stu_example {struct list_head of_node;int age;
};static LIST_HEAD(stu_list_head);
#define LIST_LEN 10int main( )
{int i = 0;/*初始化链表*/struct stu_example stu_list[LIST_LEN];struct stu_example *tmp = NULL;for  (i=0; i < LIST_LEN; i++) {list_add_tail(&stu_list[i],&stu_list_head);stu_list[i].age = i + 20;}/*遍历链表*/ list_for_each_entry(tmp, &stu_list_head, of_node) {printf("age=%d\n",tmp->age);}/*删除链表*/list_del(&stu_list_head);printf("Hello,world\n");return 0;
}

代码输出

1649a901d6d9ab4102543d88c6b19ffc.png

95704eaafa28acc905c781792668d5ab.png

讨论的重点是?

如下图

因为Linux内核用的是C89标准,不能在for循环里面声明变量,所以导致tmp变量在使用之后的代码中还可以继续使用。

继续使用并不是大问题,大问题是因为继续使用导致了一个USB的BUG,当然,从代码的结构性上来说,我觉得也应该做好封装。

cf30457bdfef7b57b33fdf93633f617a.png

9ae6a9fccd91f96ccceeeb879b4ab877.png

根据这个机制,有可能会被程序攻击到内核代码

具体可以查看这个网址

https://www.vusec.net/projects/kasper/

里面的描述和补丁说明差不多,都是因为没有遍历结束退出的原因。

d01c1db16b5938dd3939971448bf7c39.png

73965ab18d9a40cbd17abf62143be8fa.png

修改后的部分补丁

+/* Override the default implementation from linux/nospec.h. */
+#define select_nospec(cond, exptrue, expfalse) \
+({ \
+ typeof(exptrue) _out = (exptrue); \
+ \
+ asm volatile("test %1, %1\n\t"          \
+ "cmove %2, %0"            \
+ : "+r" (_out) \
+ : "r" (cond), "r" (expfalse)); \
+ _out; \
+})
+/* Prevent speculative execution past this barrier. */#define barrier_nospec() alternative("", "lfence", X86_FEATURE_LFENCE_RDTSC)diff --git a/include/linux/list.h b/include/linux/list.h
index dd6c2041d09c..1a1b39fdd122 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -636,7 +636,8 @@ static inline void list_splice_tail_init(struct list_head *list,*/#define list_for_each_entry(pos, head, member) \for (pos = list_first_entry(head, typeof(*pos), member); \
- !list_entry_is_head(pos, head, member); \
+ ({ bool _cond = !list_entry_is_head(pos, head, member); \
+ pos = select_nospec(_cond, pos, NULL); _cond; }); \pos = list_next_entry(pos, member))

具体网址:

https://lwn.net/ml/linux-kernel/20220217184829.1991035-2-jakobkoschel@gmail.com/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/464456.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

多图上传乱序php,discuz图片顺序混乱解决方案_php技巧

说明discuz在发表帖子的时候&#xff0c;添加多张图片&#xff0c;然后直接发表帖子&#xff0c;图片顺序有时候会乱掉即使上传图片窗口中图片顺序正确&#xff0c;发布之后还是会乱掉分析看url&#xff0c;程序代码中看不出什么将图片名改为序号上传&#xff0c;顺序乱了&…

在.NET中excel导出方法汇总(收集)

http://search.csdn.net/Expert/topic/2346/2346423.xml?temp.3901941http://search.csdn.net/Expert/topic/2387/2387301.xml?temp3.222293E-02http://search.csdn.net/Expert/topic/2581/2581246.xml?temp.9223444http://search.csdn.net/Expert/topic/2414/2414749.xml?…

进程之间的同步机制

多进程的系统中避免不了进程间的相互关系。本讲将介绍进程间的两种主要关系——同步与互斥&#xff0c;然后着重讲解解决进程同步的几种机制。 进程互斥是进程之间发生的一种间接性作用&#xff0c;一般是程序不希望的。通常的情况是两个或两个以上的进程需要同时访问某…

(二)AS给button添加点击事件

三种方法给Button添加点击事件 &#xff08;一&#xff09;通过button的id&#xff0c;添加继承View.OnClickListener的监听实现 <Buttonandroid:id"id/btn_button2"android:text"按钮2"android:layout_width"match_parent"android:layout_he…

MongoDB(4)--MongoDB服务的启动

原始方式 只有启动了MongoDB的服务&#xff0c;才能使用MongoDB的功能&#xff0c;通常情况下会开一个命令窗口&#xff0c;输入下面的命令来启动服务&#xff1a; 配置文件方式 如果不想每次启动的时候都在命令行中输入很多繁琐的参数&#xff0c;可以把参数信息保存在配置文件…

我张哥做的这ARM开发板,真酸爽!

本文导读&#xff1a;市场普及度最高的A7处理器&#xff0c;核心板所有设计资料、生产资料全部开放&#xff01;包含核心板原理图、PCB、BOM、uboot源码、Linux内核所有驱动源码、文件系统等&#xff0c;并提供测试与验证方案&#xff01;武汉万象奥科&#xff08;www.vanxoak.…

oracle rac维护命令,2015年oracle rac日常基本维护命令.doc

Oracle RAC 资料收集http://www.D数据库吧oracle rac日常基本维护命令2Oracle RAC性能调整12详解Oracle RAC入门和提高27ORACLE RAC数据库配置Dataguard环境49老白对于RAC应用调优的建议51oracle rac日常基本维护命令所有实例和服务的状态$ srvctl status database -d orclInst…

linux嵌入式开发流程,听听牛人怎么说

很多学习嵌入式的人来说&#xff0c;都会学习嵌入式linux开发&#xff0c;在学习的过程中&#xff0c;总会有很多难题&#xff0c;相对而言&#xff0c;嵌入式linux也算是嵌入式学习中比较难的&#xff0c;那如何可以攻破这个难点&#xff0c;那么我们就需要从根本入手&#xf…

搭建LNMP遇到的问题

配置PHP的执行./buildconf --force出现一下错误 buildconf: Your version of autoconf likely contains buggy cache code. Running vcsclean for you. To avoid this, install autoconf-2.13. 解决方案&#xff1a;安装autoconf-2.13.RPM包 执行export …

从文件中读取数据,排序之后输出到另一个文件中

文件中有一组数据&#xff0c;要求排序后输出到另一个文件中去 主要有两个知识点&#xff1a; 排序、文件操作 C/C代码如下&#xff1a; [cpp] view plaincopy #include<iostream> #include<fstream> #include<vector> using namespace std; void Or…

从单片机转到嵌入式Linux的跨度大吗?

这是我今天一个同学问我的我再零散的说一些观点&#xff0c;如果大家有这方面的经验&#xff0c;也帮忙在文章下留言&#xff0c;谢谢大家。先说共同点单片机和嵌入式他们最终都是要跑硬件的&#xff0c;所以你也会遇到像GPIO口、I2C、串口、SPI、定时器、看门狗这些问题。所以…

oracle如何取uuid,oracle如何取得uuId

是想生成GUID吗&#xff1f;SQL> select sys_guid() from dual;SYS_GUID()--------------------------------F18031C69D8345DEB305D4B2E796A282-------------------------------------------------java取得uuidpackage com.hdsoft.uuid;import java.util.UUID;public class …

有一种豁达叫开源

当人们在讨论开源的时候&#xff0c;第一时间想到的是索取&#xff0c;开源对很多开发者来说是好的事情&#xff0c;但是闭源对很多科技企业是有技术保护作用的。人们对软件的态度是经历过很多次变化的。在现代计算机研发初期&#xff0c;核心问题是硬件&#xff0c;寻找实现记…

JS应用DOM入门:DOM的对象属性

DOM提供了一套属性用于导航、访问和更新文档内容&#xff0c;其中包括只读类型的属性和可读写类型的属性。下表是只读类型的属性&#xff1a; DOM对象属性返 回 值FirstChild返回一个对象&#xff08;Object&#xff09;&#xff0c;表示第一个孩子节点&#xff08;child node&…

用O(1)的时间复杂度删除单链表中的某个节点

用O(1)的时间复杂度删除单链表中的某个节点 给定链表的头指针和一个结点指针&#xff0c;在O(1)时间删除该结点。链表结点的定义如下&#xff1a; struct ListNode {int m_nKey;ListNode* m_pNext; }; 函数的声明如下&#xff1a; void DeleteNode(ListNode* pListHea…

Django之序列化

关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户&#xff0c;特别的Ajax请求一般返回的为Json格式。 1、serializers from django.core import serializersret models.BookType.objects.all()data serializers.serialize("json", ret)2、json…

linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础

1、结构体的初始化static struct file_operations fops {.read device_read,.write device_write,.open device_open,.release device_release};以前学习C语言的时候没有见过struct的这种初始化方式。这其实是C语言的新标准。Struct一共有三种初始化的方式&#xff1a;int…

我的备忘录

http://jnesta.blogdriver.com/jnesta/index.html 转载于:https://www.cnblogs.com/oisiv/archive/2006/04/06/368663.html

怎么选工作?

选择offer&#xff0c;一直是很困难的事&#xff0c;工作不是餐桌上的美食&#xff0c;你品尝了这个菜还可以去尝那一道菜&#xff0c;所以大家都害怕因为选错一方而失去了更好的机会。而那句「选择大于努力」&#xff0c;让很多人更看重选择。我会经常遇到同学向我咨询offer选…

Selenium断言的使用,等待

自动化测试常用断言的使用方法&#xff08;python&#xff09; 自动化测试中寻找元素并进行操作&#xff0c;如果在元素好找的情况下&#xff0c;相信大家都可以较熟练地编写用例脚本了&#xff0c;但光进行操作可能还不够&#xff0c;有时候也需要对预期结果进行判断。 这里介…