{"id":279509,"date":"2023-08-06T20:06:50","date_gmt":"2023-08-06T12:06:50","guid":{"rendered":"https:\/\/lrxjmw.cn\/mrytlnhhsdyb.html"},"modified":"2023-08-06T20:06:50","modified_gmt":"2023-08-06T12:06:50","slug":"mrytlnhhsdyb","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mrytlnhhsdyb.html","title":{"rendered":"\uff08\u6bcf\u65e5\u4e00\u9898\uff09Linux\u5185\u6838\u51fd\u6570\u8c03\u7528\u8868\u65b9\u6cd5"},"content":{"rendered":"
\u76ee\u6807\uff1ahook\u51e0\u4e2aLinux<\/a>\u5185\u6838\u51fd\u6570\u8c03\u7528\uff0c\u5982\u6253\u5f00\u6587\u4ef6\u548c\u542f\u52a8\u8fdb\u7a0b\uff0c\u5e76\u501f\u52a9\u5b83\u6765\u542f\u7528\u7cfb\u7edf<\/a>\u6d3b\u52a8\u76d1\u63a7\u5e76\u62a2\u9c9c\u5236\u6b62\u53ef\u7591\u8fdb\u7a0b\u3002<\/p>\n \u4e00\u3001\u65b9\u6848\u6bd4\u8f831.\u4f7f\u7528Linux\u5b89\u5168API<\/p>\n \u65b9\u5f0f\uff1a\u5185\u6838\u4ee3\u7801\u7684\u5173\u952e\u70b9\u5305\u542b\u5b89\u5168\u51fd\u6570\u8c03\u7528linux\u7a7a\u95f4\uff0c\u8fd9\u79cd\u8c03\u7528\u53ef\u80fd\u89e6\u53d1\u5b89\u5168\u6a21\u5757\u5b89\u88c5\u7684\u53cd\u5f39\uff0c\u8be5\u6a21\u5757\u53ef\u4ee5\u5256\u6790\u7279\u5b9a\u64cd\u4f5c\u7684\u4e0a\u4e0b\u6587\uff0c\u5e76\u51b3\u5b9a\u662f\u5bb9\u8bb8\u8fd8\u662f\u4e25\u7981\u5b83\u3002<\/p>\n \u9650\u5236\uff1a\u5b89\u5168\u6a21\u5757\u672a\u80fd\u52a8\u6001\u52a0\u8f7d\uff0c\u6240\u4ee5\u987b\u8981\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\u3002<\/p>\n 2.\u66f4\u6539\u7cfb\u7edf\u8c03\u7528\u8868<\/p>\n \u65b9\u5f0f\uff1a\u6240\u6709Linux\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\u90fd\u50a8\u5b58\u5728sys_call_table\u8868\u4e2d\uff0c\u53ef\u4ee5\u4fdd\u5b58\u65e7\u7684\u5904\u7406\u7a0b\u5e8f\u503c\uff0c\u5e76\u5c06\u81ea\u5df1\u7684\u5904\u7406\u7a0b\u5e8f\u6dfb\u52a0\u5230\u8868\u4e2d\uff0c\u8fd9\u6837\u624d\u80fdhook\u4efb\u4f55\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n \u4f18\u70b9\uff1a\u4e00\u662f\u624d\u80fd\u5b8c\u5168\u63a7\u5236\u6240\u6709\u7cfb\u7edf\u8c03\u7528\uff1b\u4e8c\u662f\u6027\u80fd\u5f00\u652f\u8f83\u5c0f\uff0c\u5305\u542b\u66f4\u65b0\u7cfb\u7edf\u8c03\u7528\u8868\u3001\u76d1\u89c6\u3001\u8c03\u7528\u539f\u59cb\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\uff1b\u4e09\u662f\u901a\u7528\u6027\u8f83\u597d\uff0c\u4e0d\u4f9d\u8d56\u5185\u6838\u7248\u672c\u3002<\/p>\n \u7f3a\u70b9\uff1a\u4e00\u662f\u5b9e\u73b0\u8f83\u590d\u6742\uff0c\u9700\u67e5\u770b\u7cfb\u7edf\u8c03\u7528\u8868\u3001\u7ed5\u8fc7\u663e\u5b58\u5199\u4fdd\u62a4\u3001\u786e\u4fdd\u5904\u7406\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff1b\u4e8c\u662f\u6709\u4e9b\u5904\u7406\u7a0b\u5e8f\u6ca1\u6cd5\u66ff\u6362\uff0c\u5982\u6709\u4e9b\u4f18\u5316\u8981\u6c42\u5728\u6c47\u7f16\u4e2d\u5b9e\u73b0\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\uff1b\u4e09\u662f\u53ea\u80fdhook\u7cfb\u7edf\u8c03\u7528\u4e2d\u6807linux\uff0c\u9650\u5236\u4e86\u5165\u53e3\u70b9\u3002<\/p>\n 3.Kprobes<\/p>\n \u65b9\u5f0f\uff1a\u53ef\u4ee5\u4e3a\u4efb\u4f55\u5185\u6838\u6307\u4ee4\u3001\u51fd\u6570\u5165\u53e3\u548c\u51fd\u6570\u8fd4\u56de\u70b9\u5b89\u88c5\u5904\u7406\u7a0b\u5e8f\uff0c\u5904\u7406\u7a0b\u5e8f\u53ef\u4ee5\u8bbf\u95ee\u5bc4\u5b58\u5668\u5e76\u4fee\u6539\u5b83\u4eec\u3002<\/p>\n \u4f18\u70b9\uff1a\u4e00\u662fAPI\u5f88\u6210\u719f\uff1b\u4e8c\u662f\u80fd\u8ddf\u8e2a\u5185\u6838\u4e2d\u4efb\u610f\u70b9\uff0ckprobes\u901a\u8fc7\u5728\u5185\u6838\u4ee3\u7801\u4e2d\u5d4c\u5165\u65ad\u70b9\uff08int3\u6307\u4ee4\uff09\u5b9e\u73b0\u3002\u8ddf\u8e2a\u51fd\u6570\u5185\u90e8\u7684\u7279\u5b9a\u6307\u4ee4\u5f88\u6709\u7528\u3002<\/p>\n \u7f3a\u70b9\uff1a\u4e00\u662f\u6280\u672f\u590d\u6742\uff0c\u82e5\u8981\u83b7\u53d6\u51fd\u6570\u53c2\u6570\u6216\u5c40\u90e8\u53d8\u91cf\u503c\uff0c\u9700\u6653\u5f97\u5806\u6808\u5177\u4f53\u4f4d\u7f6e\u53ca\u6240\u5728\u5bc4\u5b58\u5668\uff0c\u5e76\u81ea\u52a8\u53d6\u51fa\uff0c\u82e5\u8981\u5236\u6b62\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u9700\u81ea\u52a8\u66f4\u6539\u8fdb\u7a0b\u72b6\u6001\uff1b\u4e8c\u662f\u5f00\u652f\u592a\u5927\uff0c\u8d85\u8fc7\u4e86\u66f4\u6539\u7cfb\u7edf\u8c03\u7528\u8868\u7684\u6210\u672c\uff1b\u4e09\u662f\u7981\u7528\u5360\u636e\uff0ckprobes\u57fa\u4e8e\u4e2d\u65ad\u548c\u6545\u969c\uff0c\u6240\u4ee5\u4e3a\u4e86\u6267\u884c\u540c\u6b65\uff0c\u6240\u6709\u5904\u7406\u7a0b\u5e8f\u9700\u4ee5\u7981\u7528\u7684\u5360\u9886\u65b9\u6cd5\u6267\u884c\uff0c\u9020\u6210\u7684\u9650\u5236\u662f\uff0c\u5904\u7406\u7a0b\u5e8f\u4e2d\u4e0d\u80fd\u7b49\u5f85\u3001\u5206\u914d\u5927\u91cf\u663e\u5b58\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3001\u5728\u8baf\u53f7\u91cf\u548c\u8ba1\u65f6\u5668\u4e2d\u4f11\u7720\u3002<\/p>\n 4.\u62fc\u63a5<\/p>\n \u65b9\u5f0f\uff1a\u5c06\u51fd\u6570\u5f00\u5934\u7684\u6307\u4ee4\u66ff\u6362\u4e3a\u901a\u5411\u5904\u7406\u7a0b\u5e8f\u7684\u65e0\u6761\u4ef6\u8df3\u8f6c\uff0c\u5904\u7406\u5b8c\u6210\u540e\u518d\u6267\u884c\u539f\u59cb\u6307\u4ee4\uff0c\u518d\u632a\u5230\u622a\u65ad\u51fd\u6570\u524d\u6267\u884c\u3002\u7c7b\u4f3c\u4e8ekprobes\u3002<\/p>\n \u4f18\u70b9\uff1a\u4e00\u662f\u4e0d\u987b\u8981\u8bbe\u7f6e\u5185\u6838\u7f16\u8bd1\u9009\u9879\uff0c\u53ef\u5728\u4efb\u4f55\u51fd\u6570\u5f00\u5934\u5b9e\u73b0\uff1b\u4e8c\u662f\u5f00\u652f\u4f4e\uff0c\u4e24\u6b21\u8df3\u8f6c\u5373\u53ef\u8fd4\u56de\u5230\u539f\u59cb\u70b9\u3002<\/p>\n \u7f3a\u70b9\uff1a\u6280\u672f\u590d\u6742\u3002<\/p>\n \u4e8c\u3001\u4f7f\u7528Ftracehook\u51fd\u65701.\u7b80\u4ecb<\/p>\n ftrace\u63d0\u4f9b\u597d\u591a\u51fd\u6570\u96c6\uff0c\u53ef\u663e\u793a\u8c03\u7528\u56fe\u3001\u8ddf\u8e2a\u51fd\u6570\u8c03\u7528\u9891\u5ea6\u548c\u539a\u5ea6\u3001\u8fc7\u6ee4\u7279\u5b9a\u51fd\u6570\u3002ftrace\u7684\u5b9e\u73b0\u57fa\u4e8e\u7f16\u8bd1\u5668\u9009\u9879-pg\u548c-mfentry\uff0c\u8fd9\u79cd\u5185\u6838\u9009\u9879\u5728\u6bcf\u4f4d\u51fd\u6570\u7684\u5f00\u5934\u63d2\u5165\u4e00\u4e2a\u7279\u6b8a\u8ddf\u8e2a\u51fd\u6570\u7684\u8c03\u7528\u2014mcount()\u6216fentry()\uff0c\u7528\u4e8e\u5b9e\u73b0ftrace\u6846\u67b6\u3002<\/p>\n \u800c\u4e14\u6bcf\u4f4d\u51fd\u6570\u8c03\u7528ftrace\u4f1a\u4f7f\u6027\u80fd\u51cf\u5c11\uff0c\u6240\u4ee5\u6709\u4e00\u79cd\u4f18\u5316\u673a\u5236\u2014\u2014\u52a8\u6001trace\u3002\u5185\u6838\u6653\u5f97\u8c03\u7528mcount()\u6216fentry()\u7684\u4f4d\u7f6e\uff0c\u5728\u521d\u671f\u9636\u6bb5\u5c06\u673a\u5668\u7801\u66ff\u6362\u4e3anop\uff0c\u5f53\u6253\u5f00Linux\u5185\u6838\u8ddf\u8e2a\u65f6\uff0cftrace\u8c03\u7528\u4f1a\u88ab\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u51fd\u6570\u4e2d\u3002<\/p>\n 2.\u51fd\u6570\u8bf4\u660e<\/p>\n \u4ee5\u4e0b\u7ed3\u6784\u7528\u4e8e\u63cf\u8ff0\u6bcf\u4f4d\u94a9\u5b50\u51fd\u6570\uff1a<\/p>\n \n \u53ef\u4ee5\u53ea\u586b\u5199\u4e09\u4e2a\u6570\u7ec4\uff1aname\u3001function\u3001original\u3002<\/p>\n \n \u94a9\u5b50\u51fd\u6570\u5305\u88c5\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n \n 3.\u521d\u59cb\u5316ftrace<\/p>\n \u7b2c\u4e00\u6b65\u662f\u67e5\u627e\u88abhook\u51fd\u6570\u7684\u5730\u5740\uff0c\u901a\u8fc7kallsyms\u3002<\/p>\n <\/p>\n \n \u7b2c\u4e8c\u6b65\uff0c\u521d\u59cb\u5316ftrace_ops\u7ed3\u6784\uff0c\u9700\u8bbe\u7f6e\u5fc5\u8981\u6570\u7ec4funcflags\u3002<\/p>\n \n fh_ftrace_thunk()\u662fftrace\u5728\u8ddf\u8e2a\u51fd\u6570\u65f6\u7684\u53cd\u5f39\u51fd\u6570\uff0c\u7a0d\u540e\u8ba8\u8bba\u3002flags\u610f\u4e49\u662f\u544a\u8bc9ftrace\u4fdd\u5b58\u548c\u6062\u590d\u5bc4\u5b58\u5668\uff08\u4ee5\u66f4\u6539\u5bc4\u5b58\u5668\uff09\uff0c\u6211\u4eec\u53ef\u5728\u53cd\u5f39\u51fd\u6570\u4e2d\u66f4\u6539\u8fd9\u79cd\u5bc4\u5b58\u5668\u7684\u5185\u5bb9\uff08RIP\uff09\u3002<\/p>\n \u7b2c\u4e09\u6b65\uff0c\u5f00\u59cbhook\uff0c\u9996\u5148\u7528ftrace_set_filter_ip()\u4e3a\u987b\u8981\u8ddf\u8e2a\u7684\u51fd\u6570\u6253\u5f00ftrace\uff0c\u518d\u8c03\u7528register_ftrace_function()\u5bf9\u88abhook\u51fd\u6570\u8fdb\u884c\u6ce8\u518c\u3002\u8bb0\u5f97\u7528ftrace_set_filter_ip()\u5173\u6389ftrace\u3002<\/p>\n \n \u5173\u6389\u94a9\u5b50\u5982\u4e0b\uff0c\u9632\u6b62\u94a9\u5b50\u51fd\u6570\u4e00\u76f4\u5728\u5176\u4ed6\u5730\u65b9\u6267\u884c\uff1a<\/p>\n \n 4.\u7528ftracehook\u51fd\u6570<\/p>\n <\/p>\n \u539f\u7406\uff1a\u66f4\u6539rip\u5bc4\u5b58\u5668\u6307\u5411\u81ea\u5b9a\u4e49\u7684\u53cd\u5f39\u51fd\u6570\u3002<\/p>\n \n 5.\u907f\u514d\u9012\u5f52\u8c03\u7528<\/p>\n \u95ee\u9898\uff1a\u5f53\u5305\u88c5\u51fd\u6570\u8c03\u7528\u539f\u59cb\u51fd\u6570\u65f6\uff0c\u539f\u59cb\u51fd\u6570\u5c06\u88abftrace\u518d\u5ea6\u8ddf\u8e2a\uff0c\u8fdb\u800c\u9020\u6210\u65e0\u7a77\u65e0\u5c3d\u7684\u9012\u5f52\u3002<\/p>\n \u89e3\u51b3\uff1a\u53ef\u501f\u52a9parent_ip\uff08\u8c03\u7528\u94a9\u5b50\u51fd\u6570\u7684\u8fd4\u56de\u5730\u5740\uff09\u2014\u2014ftrace\u53cd\u5f39\u53c2\u6570\u4e4b\u4e00\uff0c\u8be5\u53c2\u6570\u4e00\u822c\u7528\u4e8e\u5efa\u7acb\u51fd\u6570\u8c03\u7528\u56fe\uff0c\u4f46\u4e5f\u53ef\u4ee5\u62ff\u6765\u5206\u8fa8\u8ddf\u8e2a\u51fd\u6570\u662f\u7b2c\u4e00\u6b21\u8c03\u7528\u8fd8\u662f\u91cd\u590d\u8c03\u7528\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0cparent_ip\u6307\u5411\u5185\u6838\u67d0\u4e2a\u4f4d\u7f6e\uff0c\u91cd\u590d\u8c03\u7528\u65f6\uff0cparent_ip\u6307\u5411\u5305\u88c5\u51fd\u6570\u5185\u90e8\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u6267\u884c\u53cd\u5f39\u51fd\u6570linux \u8c03\u7528\u5185\u6838\u51fd\u6570<\/strong>linux \u8c03\u7528\u5185\u6838\u51fd\u6570<\/strong>\uff0c\u5176\u4ed6\u8c03\u7528\u65f6\u9700\u6267\u884c\u539f\u59cb\u51fd\u6570\u3002<\/p>\n \n \u5185\u6838\u4e2dhook\u51fd\u6570\u7684\u6574\u4f53\u6267\u884c\u6d41\u7a0b\u53ef\u53c2\u89c1Hookinglinux\u5185\u6838\u51fd\u6570\uff08\u4e8c\uff09\uff1a\u600e\u6837\u4f7f\u7528Ftracehook\u51fd\u6570\u4e2d\u7684\u56fe\u793a\u3002<\/p>\n \u4e09\u3001Ftrace\u8bc4\u4ef7\u4e0e\u914d\u7f6e1.\u5f02\u540c\u70b9<\/p>\n \u4f18\u70b9\uff1a\u4e00\u662fAPI\u6210\u719f\uff0c\u4ee3\u7801\u7b80\u5355\uff1b\u4e8c\u662f\u6309\u7167\u540d\u79f0\u80fd\u591f\u8ddf\u8e2a\u4efb\u4f55\u51fd\u6570\uff1b\u4e09\u662f\u5f00\u652f\u8f83\u4f4e\u3002<\/p>\n \u7f3a\u70b9\uff1a\u4e00\u662f\u914d\u7f6e\u4e0a\u6709\u8981\u6c42\uff0c\u9700\u652f\u6301kallsyms\u51fd\u6570\u7d22\u5f15\u3001ftrace\u6846\u67b6\uff1b\u4e8c\u662fftrace\u53ea\u80fd\u5728\u51fd\u6570\u5165\u53e3\u70b9\u5de5\u4f5c\u3002<\/p>\n 2.\u914d\u7f6e<\/p>\n \u7f16\u8bd1\u5185\u6838\u65f6\u9700\u8bbe\u7f6e\u4ee5\u4e0b\u9009\u9879\uff1a<\/p>\n 3.\u542f\u53d1<\/p>\n \u542f\u53d1\uff1a\u53ef\u4ee5hook\u4e2a\u522b\u51fd\u6570\uff0c\u5982\u5806\u5206\u914d\u51fd\u6570kmalloc\uff0c\u8bb0\u5f55\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n \u53c2\u8003<\/p>\n \u793a\u4f8b\u4ee3\u7801<\/p>\n Hookinglinux\u5185\u6838\u51fd\u6570\uff08\u4e00\uff09\uff1a\u627e\u5bfb\u5b8c\u7f8e\u89e3\u51b3\u65b9\u6848<\/p>\n Hookinglinux\u5185\u6838\u51fd\u6570\uff08\u4e8c\uff09\uff1a\u600e\u6837\u4f7f\u7528Ftracehook\u51fd\u6570<\/p>\n Hookinglinux\u5185\u6838\u51fd\u6570\uff08\u4e09\uff09\uff1aFtrace\u7684\u4e3b\u8981\u5f02\u540c\u70b9<\/p>\n","protected":false},"excerpt":{"rendered":" ()\u7684\u4f4d\u7f6e\uff0c\u5728\u65e9\u671f\u9636\u6bb5\u5c06\u673a\u5668\u7801\u66ff\u6362\u4e3anop\uff0c\u5f53\u6253\u5f00Linux\u5185\u6838\u8ddf\u8e2a\u65f6\uff0cftrace\u8c03\u7528\u4f1a\u88ab\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u51fd\u6570\u4e2d\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0cparent_ip\u6307\u5411\u5185\u6838\u67d0\u4e2a\u4f4d\u7f6e\uff0c\u91cd\u590d\u8c03\u7528\u65f6\uff0cparent_ip\u6307\u5411\u5305\u88c5\u51fd\u6570\u5185\u90e8\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u6267\u884c\u56de\u8c03\u51fd\u6570\uff0c\u5176\u4ed6\u8c03\u7528\u65f6\u9700\u6267\u884c\u539f\u59cb\u51fd\u6570\u3002linux\u5185\u6838\u51fd\u6570\uff08\u4e00\uff09\uff1a\u5bfb\u627e\u5b8c\u7f8e\u89e3\u51b3\u65b9\u6848<\/p>\n","protected":false},"author":1,"featured_media":279510,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-279509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/279509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=279509"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/279509\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/279510"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=279509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=279509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=279509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\/*\nname: \u88abhook\u7684\u51fd\u6570\u540d\nfunction: \u94a9\u5b50\u51fd\u6570\u7684\u5730\u5740(\u66ff\u4ee3\u88abhook\u51fd\u6570)\noriginal: \u6307\u9488\uff0c\u6307\u5411\u5b58\u50a8\u88abhook\u51fd\u6570\u7684\u5730\u5740\u7684\u5730\u65b9\naddress: \u88abhook\u51fd\u6570\u7684\u5730\u5740\nops: ftrace\u670d\u52a1\u4fe1\u606f\n*\/\nstruct ftrace_hook {\n const char *name;\n void *function;\n void *original;\n unsigned long address;\n struct ftrace_ops ops;\n};\n<\/code><\/pre>\n<\/p>\n
#define HOOK(_name, _function, _original) \n { \n .name = (_name), \n .function = (_function), \n .original = (_original), \n }\nstatic struct ftrace_hook hooked_functions[] = {\n HOOK(\"sys_clone\", fh_sys_clone, &real_sys_clone),\n HOOK(\"sys_execve\", fh_sys_execve, &real_sys_execve),\n};\n<\/code><\/pre>\n<\/p>\n
\/*\n\u8fd9\u662f\u4e2a\u6307\u5411\u539f\u59cbexecve()\u7684\u6307\u9488\uff0c\u53ef\u88abwrapper\u8c03\u7528\u3002\u672a\u6539\u53d8\u53c2\u6570\u987a\u5e8f\u548c\u7c7b\u578b\u3001\u8fd4\u56de\u503c\nasmlinkage:\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\u53c2\u6570\u4e0d\u662f\u901a\u8fc7\u6808\u4f20\u9012\uff0c\u800c\u662f\u76f4\u63a5\u653e\u5230\u5bc4\u5b58\u5668\u91cc\n *\/\nstatic asmlinkage long (*real_sys_execve)(const char __user *filename,\n const char __user *const __user *argv,\n const char __user *const __user *envp);\n\/*\nfh_sys_execve\u51fd\u6570\u5c06\u4ee3\u66ff\u88abhook\u51fd\u6570\u6267\u884c\uff0c\u5b83\u7684\u53c2\u6570\u548c\u539f\u59cb\u51fd\u6570\u4e00\u6837\uff0c\u8fd4\u56de\u503c\u4e5f\u6b63\u5e38\u8fd4\u56de\u3002\u8be5\u51fd\u6570\u53ef\u5728\u88abhook\u51fd\u6570\u4e4b\u524d\u3001\u4e4b\u540e\u6216\u66ff\u4ee3\u6267\u884c\u3002\n *\/\nstatic asmlinkage long fh_sys_execve (const char __user *filename,\n const char __user *const __user *argv,\n const char __user *const __user *envp)\n{\n long ret;\n pr_debug(\"execve() called: filename=%p argv=%p envp=%pn\",\n filename, argv, envp);\n ret = real_sys_execve(filename, argv, envp);\n pr_debug(\"execve() returns: %ldn\", ret);\n return ret;\n}\n<\/code><\/pre>\n<\/p>\n
static int resolve_hook_address (struct ftrace_hook *hook)\n hook->address = kallsyms_lookup_name(hook->name);\n if (!hook->address) {\n pr_debug(\"unresolved symbol: %sn\", hook->name);\n return -ENOENT;\n }\n *((unsigned long*) hook->original) = hook->address;\n return 0;\n}\n<\/code><\/pre>\n<\/p>\n
int fh_install_hook (struct ftrace_hook *hook)\n int err;\n err = resolve_hook_address(hook);\n if (err)\n return err;\n hook->ops.func = fh_ftrace_thunk;\n hook->ops.flags = FTRACE_OPS_FL_SAVE_REGS\n | FTRACE_OPS_FL_IPMODIFY;\n \/* ... *\/\n}\n<\/code><\/pre>\n<\/p>\n
int fh_install_hook (struct ftrace_hook *hook)\n{\n \/* ... *\/\n err = ftrace_set_filter_ip(&hook->ops, hook->address, 0, 0);\n if (err) {\n pr_debug(\"ftrace_set_filter_ip() failed: %dn\", err);\n return err;\n }\n err = register_ftrace_function(&hook->ops);\n if (err) {\n pr_debug(\"register_ftrace_function() failed: %dn\", err);\n \/* Don\u2019t forget to turn off ftrace in case of an error. *\/\n ftrace_set_filter_ip(&hook->ops, hook->address, 1, 0); \n return err;\n }\n return 0;\n}\n<\/code><\/pre>\n<\/p>\n
void fh_remove_hook (struct ftrace_hook *hook)\n{\n int err;\n err = unregister_ftrace_function(&hook->ops);\n if (err)\n pr_debug(\"unregister_ftrace_function() failed: %dn\", err);\n }\n err = ftrace_set_filter_ip(&hook->ops, hook->address, 1, 0);\n if (err) {\n pr_debug(\"ftrace_set_filter_ip() failed: %dn\", err);\n }\n}\n<\/code><\/pre>\n<\/p>\n
static void notrace fh_ftrace_thunk(unsigned long ip, unsigned long parent_ip,\n struct ftrace_ops *ops, struct pt_regs *regs)\n{\n struct ftrace_hook *hook = container_of(ops, struct ftrace_hook, ops);\n regs->ip = (unsigned long) hook->function;\n}\n\/\/ container_of(ptr,type,member): \u5df2\u77e5\u7ed3\u6784\u4f53type\u7684\u6210\u5458member\u7684\u5730\u5740ptr\uff0c\u6c42\u89e3\u7ed3\u6784\u4f53type\u7684\u8d77\u59cb\u5730\u5740\u3002\u4e5f\u5373\u8fd4\u56deftrace_hook\u7ed3\u6784\u7684\u9996\u5730\u5740\u3002\n\/\/ notrace\u8bf4\u660e\u7b26\uff1a\u5982\u679c\u4e0d\u5c0f\u5fc3\u4eceftrace\u56de\u8c03\u4e2d\u8c03\u7528\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u7cfb\u7edf\u5c31\u4e0d\u4f1a\u6302\u8d77\uff0c\u56e0\u4e3aftrace\u6b63\u5728\u8ddf\u8e2a\u8fd9\u4e2a\u51fd\u6570\u3002\n<\/code><\/pre>\n<\/p>\n
static void notrace fh_ftrace_thunk (unsigned long ip, unsigned long parent_ip,\n struct ftrace_ops *ops, struct pt_regs *regs)\n{\n struct ftrace_hook *hook = container_of(ops, struct ftrace_hook, ops);\n \/* Skip the function calls from the current module. *\/\n if (!within_module(parent_ip, THIS_MODULE))\n regs->ip = (unsigned long) hook->function;\n}\n<\/code><\/pre>\n<\/p>\n