{"id":109,"date":"2016-12-16T08:24:57","date_gmt":"2016-12-16T08:24:57","guid":{"rendered":"http:\/\/www.luo666.com\/?p=109"},"modified":"2019-01-19T04:44:29","modified_gmt":"2019-01-19T04:44:29","slug":"ptrace%e7%9a%84%e6%9d%83%e9%99%90%e6%a3%80%e6%9f%a5%ef%bc%8c%e8%87%aa%e7%94%b1%e6%98%af%e6%9c%89%e5%89%8d%e6%8f%90%e7%9a%84","status":"publish","type":"post","link":"http:\/\/www.luo666.com\/?p=109","title":{"rendered":"PTRACE\u7684\u6743\u9650\u68c0\u67e5\uff0c\u81ea\u7531\u662f\u6709\u524d\u63d0\u7684~"},"content":{"rendered":"<div>\u7cfb\u7edf\u8c03\u7528PTRACE\u7684\u4e00\u5207\u5f00\u59cb\u4e8e\u8fd9\u91cc\uff1a<\/div>\n<div>COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0compat_long_t, addr, compat_long_t, data)<\/div>\n<div>\n<hr \/>\n<\/div>\n<div>\u9996\u5148\u63d0\u4f9b\u4e86attach\u7684\u7eff\u8272\u901a\u9053\uff1a<\/div>\n<div>if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 ret = ptrace_attach(child, request, addr, data);<\/div>\n<div>attach\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u628a\u5f53\u524d\u8fdb\u7a0b\u53d8\u6210\u4e86\u88abtrace\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\uff0c\u540c\u65f6\u628a\u88abtrace\u8fdb\u7a0b\u52a0\u5165\u5230\u5f53\u524d\u8fdb\u7a0b\u7684ptraced\u94fe\u8868\u4e2d\uff1b<\/div>\n<div><b>__ptrace_link<\/b>(task, current);<\/div>\n<div>list_add(&amp;child-&gt;ptrace_entry, &amp;new_parent-&gt;ptraced);<\/div>\n<div>\u00a0 \u00a0 child-&gt;parent = new_parent;<\/div>\n<div>\u4f46\u662f\u8fd9\u6837\u505a\u662f\u6709\u524d\u63d0\u7684\uff0c\u5177\u4f53\u8bf7\u770b\u540e\u9762\u7684\u5206\u6790\uff0c\u8fd9\u91cc\u5148\u6309\u4e0b\u4e0d\u8868\u3002<\/div>\n<div>\n<hr \/>\n<\/div>\n<div>attach\uff1fwtf\uff1f\u4e3a\u5565\u8981\u5728\u7528\u4e4b\u524dattach\uff0c\u641e\u5f97\u8fd9\u4e48\u795e\u79d8\uff0c\u96be\u9053\u76f4\u63a5\u7528\u4e0d\u53ef\u4ee5\u5417\uff1f<\/div>\n<div>\u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u5426\u5219\u5730\u5740\u7a7a\u95f4\u7684\u9694\u79bb\u4e0d\u5c31\u53d8\u6210\u7b11\u8bdd\u4e86\u3002<\/div>\n<div><\/div>\n<div>\u6240\u4ee5\u8fd9\u4e2aptrace\u7cfb\u7edf\u8c03\u7528\u5165\u53e3\u505a\u4e86\u4e2a\u5f88\u91cd\u8981\u7684check\uff1a<\/div>\n<div>ret = <b>ptrace_check_attach<\/b>(child, request == PTRACE_KILL ||<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 request == PTRACE_INTERRUPT);<\/div>\n<div>\u00a0 \u00a0 if (!ret) { \/\/\u53ea\u6709\u8fd4\u56de\u4e3a0\uff0c\u5373\u5224\u65ad\u6761\u4ef6\u6210\u7acb\uff0c\u624d\u7ee7\u7eed\u6267\u884c\u5bf9ptrace\u7684request<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 ret = compat_arch_ptrace(child, request, addr, data);<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 if (ret || request != PTRACE_DETACH)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ptrace_unfreeze_traced(child);<\/div>\n<div>\u00a0 \u00a0 }<\/div>\n<div><\/div>\n<div>child-&gt;ptrace &amp;&amp; child-&gt;parent == current<\/div>\n<div>\u53ea\u6709\u8fc7\u4e86\u8fd9\u4e00\u5173\uff0c\u624d\u80fd\u8fdb\u884c\u540e\u9762\u7684\u64cd\u4f5c\uff08\u9664\u4e86kil\u548cinterrupt\uff0c\u4ed6\u4eec\u81ea\u5df1\u7684\u5904\u7406\u903b\u8f91\u91cc\u9762\u6709\u6b64\u7c7b\u7684\u5224\u65ad\u6216\u8005\u65e0\u9700\uff09\uff0c\u90a3\u96be\u9053\u968f\u4fbf\u5c31\u53ef\u4ee5attach\uff0c\u90a3\u6211\u5c31\u9ebb\u70e6\u4e00\u70b9\u5148attach\u5457\uff1b\u663e\u7136\u4e0d\u4f1a\u8fd9\u4e48\u50bb\uff0c\u524d\u9762\u5356\u4e86\u4e2a\u5173\u5b50\uff0c\u8fd9\u91cc\u6765\u770b\u770battach\u91cc\u9762\u505a\u7684\u6743\u9650\u68c0\u67e5\uff0c\u5fc5\u987b\u662f\u6709\u6743\u7684\u4eba\u624d\u80fd\u968f\u4fbf\u641e\u7684\uff0c\u5c41\u6c11\u662f\u8981\u5b88\u6cd5\u7684\uff01\uff01<\/div>\n<div>\n<hr \/>\n<\/div>\n<div>\u6743\u9650\u7ba1\u7406\u7684\u6c34\u6bd4\u8f83\u6df1\uff0c\u535a\u4e3b\u8fd8\u6ca1\u5403\u900f\uff0c\u6240\u4ee5\u8fd9\u91cc\u5148\u7b80\u5355\u5217\u4e00\u4e0b\u6211\u80fd\u770b\u61c2\u7684\u90e8\u5206\uff1a<\/div>\n<div>etval = <b>__ptrace_may_access<\/b>(task, PTRACE_MODE_ATTACH);<\/div>\n<div>\u8fd9\u91cc\u6709\u4e24\u4e2a\u6761\u4ef6\uff0c1\uff09\u6211\u81ea\u5df1\u4eba\u6539\u81ea\u5df1\u4ebaok\uff08\u8fd9\u91cc\u7684suid euid\u7b49\u7b49\u7684\u8bf7\u770b\u540e\u9762\u94fe\u63a5\uff09\uff1b2\uff09\u6211\u7684\u4e0a\u5c42\u547d\u540d\u7a7a\u95f4ok\uff08\u6211\u7406\u89e3\u662f\uff0c\u6bd4\u5982\u50cfroot\u7528\u6237\u8fd9\u79cd\u5927boss\uff09<\/div>\n<div>\u00a0 \u00a0 tcred = __task_cred(task);<\/div>\n<div>\u00a0 \u00a0 if (uid_eq(cred-&gt;uid, tcred-&gt;euid) &amp;&amp;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 uid_eq(cred-&gt;uid, tcred-&gt;suid) &amp;&amp;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 uid_eq(cred-&gt;uid, tcred-&gt;uid)\u00a0 &amp;&amp;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 gid_eq(cred-&gt;gid, tcred-&gt;egid) &amp;&amp;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 gid_eq(cred-&gt;gid, tcred-&gt;sgid) &amp;&amp;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 gid_eq(cred-&gt;gid, tcred-&gt;gid))<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 goto ok;<\/div>\n<div>\u00a0 \u00a0 if (ptrace_has_cap(tcred-&gt;user_ns, mode))<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 goto ok;<\/div>\n<div>\u53e6\u5916\u8fd8\u8981\u505a\u547d\u540d\u7a7a\u95f4\u80fd\u529b\u7684\u68c0\u6d4b\uff0c\u770b\u770b\u76ee\u6807\u8fdb\u7a0b\u7684\u547d\u540d\u7a7a\u95f4\u6709\u6ca1\u6709\u88abptrace\u7684\u80fd\u529b\uff1a<\/div>\n<div>ns_capable(__task_cred(task)-&gt;user_ns, CAP_SYS_PTRACE)<\/div>\n<div>\n<hr \/>\n<\/div>\n<div>\u53c2\u8003\u94fe\u63a5<\/div>\n<div><a href=\"http:\/\/stackoverflow.com\/questions\/8499296\/realuid-saved-uid-effective-uid-whats-going-on\">http:\/\/stackoverflow.com\/questions\/8499296\/realuid-saved-uid-effective-uid-whats-going-on<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7cfb\u7edf\u8c03\u7528PTRACE\u7684\u4e00\u5207\u5f00\u59cb\u4e8e\u8fd9\u91cc\uff1a COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0compat_long_t, addr, compat_long_t, data) \u9996\u5148\u63d0\u4f9b\u4e86attach\u7684\u7eff\u8272\u901a\u9053\uff1a if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { \u00a0 \u00a0 \u00a0 \u00a0 ret = ptrace_attach(child, request, addr, data); attach\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u628a\u5f53\u524d\u8fdb\u7a0b\u53d8\u6210\u4e86\u88abtrace\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\uff0c\u540c\u65f6\u628a\u88abtrace\u8fdb\u7a0b\u52a0\u5165\u5230\u5f53\u524d\u8fdb\u7a0b\u7684ptraced\u94fe\u8868\u4e2d\uff1b __ptrace_link(task, current); list_add(&amp;child-&gt;ptrace_entry, &amp;new_parent-&gt;ptraced); \u00a0 \u00a0 child-&gt;parent = new_parent; \u4f46\u662f\u8fd9\u6837\u505a\u662f\u6709\u524d\u63d0\u7684\uff0c\u5177\u4f53\u8bf7\u770b\u540e\u9762\u7684\u5206\u6790\uff0c\u8fd9\u91cc\u5148\u6309\u4e0b\u4e0d\u8868\u3002 attach\uff1fwtf\uff1f\u4e3a\u5565\u8981\u5728\u7528\u4e4b\u524dattach\uff0c\u641e\u5f97\u8fd9\u4e48\u795e\u79d8\uff0c\u96be\u9053\u76f4\u63a5\u7528\u4e0d\u53ef\u4ee5\u5417\uff1f \u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3a\u4f60\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u5176\u4ed6\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u5426\u5219\u5730\u5740\u7a7a\u95f4\u7684\u9694\u79bb\u4e0d\u5c31\u53d8\u6210\u7b11\u8bdd\u4e86\u3002 \u6240\u4ee5\u8fd9\u4e2aptrace\u7cfb\u7edf\u8c03\u7528\u5165\u53e3\u505a\u4e86\u4e2a\u5f88\u91cd\u8981\u7684check\uff1a ret = ptrace_check_attach(child, request &hellip; <a href=\"http:\/\/www.luo666.com\/?p=109\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PTRACE\u7684\u6743\u9650\u68c0\u67e5\uff0c\u81ea\u7531\u662f\u6709\u524d\u63d0\u7684~&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7Dhki-1L","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/109"}],"collection":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":1,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}