DEVICE_ATTR设置0777引发血案
生活随笔
收集整理的這篇文章主要介紹了
DEVICE_ATTR设置0777引发血案
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這個宏我們在內(nèi)核里面使用非常頻繁,這個宏的作用可以拋出sys設備節(jié)點給用戶使用。用戶可以讀寫sys/class下面的文件節(jié)點,以達到控制內(nèi)核驅(qū)動的功效。
比如,像這樣的設備節(jié)點
使用方法
static?ssize_t?gpio_store_en(struct?device?*dev,struct?device_attribute?*attr,const?char?*buf,?size_t?count) {struct?gpio_dev_data?*dev_data?=?dev_get_drvdata(dev);unsigned?long?value?=?0;int?ret;/*將echo進來的buf轉換成整型*/ret?=?kstrtoul(buf,?16,?&value);if?(ret?<?0)?{printk(?"%s:kstrtoul?failed,?ret=%d\n",?__func__,?ret);return?ret;}printk("%s:?en?value?:?%d\n",?__func__,?(int)value);if?(value)?{gpio_direction_output(dev_data->en_pin,?dev_data->en_val);dev_data->gpio_val?=?1;}?else?{gpio_direction_output(dev_data->en_pin,?!dev_data->en_val);dev_data->gpio_val?=?0;}return?count; }static??char?mybuf[10]="123";/*cat命令時,將會調(diào)用該函數(shù)*/ static?ssize_t?gpio_show_en(struct?device?*dev,struct?device_attribute?*attr,?char?*buf) {struct?gpio_dev_data?*dev_data?=?dev_get_drvdata(dev);snprintf(mybuf,sizeof(mybuf),"%d",dev_data->gpio_val);return?sprintf(buf,?"%s\n",?mybuf); }static?DEVICE_ATTR(gpio_en,0664,gpio_show_en,?gpio_store_en);...dev_class?=?class_create(THIS_MODULE,?class_name); ctl_dev?=?device_create(dev_class,?NULL,?0,?NULL,?"onoff"); if?(IS_ERR(ctl_dev))?{dev_err(ctl_dev,?"Failed?to?create?device\n");ret?=?PTR_ERR(ctl_dev);goto?err_create_dev; }err?=?device_create_file(ctl_dev,?&dev_attr_gpio_en); if?(err){printk("driver_create_file?=?%d\n",?err); }DEVICE_ATTR 0777 引發(fā)的血案
如果你想給一個節(jié)點設置 0777 或者寫操作,那你編譯的時候,會出現(xiàn)下面的編譯錯誤。
/home/weiqifa/mt8167s-9.0-sdk/kernel-4.4/include/linux/kernel.h:840:3:?note:?in?expansion?of?macro?'BUILD_BUG_ON_ZERO'BUILD_BUG_ON_ZERO((perms)?&?2)?+?????\^ /home/weiqifa/mt8167s-9.0-sdk/kernel-4.4/include/linux/sysfs.h:102:12:?note:?in?expansion?of?macro?'VERIFY_OCTAL_PERMISSIONS'.mode?=?VERIFY_OCTAL_PERMISSIONS(_mode)?},??\^ /home/weiqifa/mt8167s-9.0-sdk/kernel-4.4/include/linux/device.h:573:45:?note:?in?expansion?of?macro?'__ATTR'struct?device_attribute?dev_attr_##_name?=?__ATTR(_name,?_mode,?_show,?_store)^ /home/weiqifa/mt8167s-9.0-sdk/kernel-4.4/drivers/misc/gpio_control.c:62:8:?note:?in?expansion?of?macro?'DEVICE_ATTR'static?DEVICE_ATTR(gpio_en,0777,gpio_show_en,?gpio_store_en);這個錯誤的原因主要是出現(xiàn)在
VERIFY_OCTAL_PERMISSIONS這個宏上面
這個宏定義在
include/linux/下面
/*?Permissions?on?a?sysfs?file:?you?didn't?miss?the?0?prefix?did?you??*/ #define?VERIFY_OCTAL_PERMISSIONS(perms)??????\(BUILD_BUG_ON_ZERO((perms)?<?0)?+?????\BUILD_BUG_ON_ZERO((perms)?>?0777)?+?????\/*?USER_READABLE?>=?GROUP_READABLE?>=?OTHER_READABLE?*/??\BUILD_BUG_ON_ZERO((((perms)?>>?6)?&?4)?<?(((perms)?>>?3)?&?4))?+?\BUILD_BUG_ON_ZERO((((perms)?>>?3)?&?4)?<?((perms)?&?4))?+??\/*?USER_WRITABLE?>=?GROUP_WRITABLE?*/?????\BUILD_BUG_ON_ZERO((((perms)?>>?6)?&?2)?<?(((perms)?>>?3)?&?2))?+?\/*?OTHER_WRITABLE???Generally?considered?a?bad?idea.?*/??\BUILD_BUG_ON_ZERO((perms)?&?2)?+?????\(perms)) #endifBUILD_BUG_ON_ZERO 的作用
這個宏的作用是,如果里面?zhèn)鬟M來的值是 「true」編譯的時候就會出錯。
寫個代碼舉個例子
#include?<stdio.h> #include?<stdbool.h> //#define?BUILD_BUG_ON(condition)?((void)sizeof(char[1?-?2*!!(condition)])) /*?Force?a?compilation?error?if?condition?is?true?*/ #define?BUILD_BUG_ON(condition)?((void)BUILD_BUG_ON_ZERO(condition)) /*?Force?a?compilation?error?if?condition?is?true,?but?also?produce?aresult?(of?value?0?and?type?size_t),?so?the?expression?can?be?usede.g.?in?a?structure?initializer?(or?where-ever?else?comma?expressionsaren't?permitted).?*/ #define?BUILD_BUG_ON_ZERO(e)?(sizeof(struct?{?int:-!!(e);?})) #define?BUILD_BUG_ON_NULL(e)?((void?*)sizeof(struct?{?int:-!!(e);?})) int?main() {BUILD_BUG_ON(1!=0);bool?zero?=?false;printf("%d\n",?!!zero);printf("%d\n",?!zero);return?0; }輸出
VERIFY_OCTAL_PERMISSIONS 的作用?
所以在回到這個宏,這個宏的作用就是限制我們在內(nèi)核里面設置DEVICE_ATTR的權限,如果你要是設置 0777,那肯定就會給你提示編譯錯誤。
0777 對應的是 8進制
整個流程
整個流程是如上圖,代碼是在mode部分那里做了限制。
怎么讓DEVICE_ATTR 0777 生效?
既然我們知道是
VERIFY_OCTAL_PERMISSIONS 這個宏限制的,那就直接把這個宏修改就好了。
當然了,這樣使用是不符合要求的,如果這樣,很容易被裁員的哦,畢竟用戶可能隨便寫一段代碼就可能讓你的系統(tǒng)不正常。
看燒錄看效果
? 推薦閱讀:
??專輯|Linux文章匯總
??專輯|程序人生
??專輯|C語言
嵌入式Linux
微信掃描二維碼,關注我的公眾號
總結
以上是生活随笔為你收集整理的DEVICE_ATTR设置0777引发血案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一款强大的反编译工具luyten
- 下一篇: python房价数据分析波士顿_Pyth