{"id":606,"date":"2025-07-27T23:12:46","date_gmt":"2025-07-27T15:12:46","guid":{"rendered":"http:\/\/pareto.fun\/?p=606"},"modified":"2025-07-27T23:12:47","modified_gmt":"2025-07-27T15:12:47","slug":"%e8%a6%81%e5%8d%81%e5%b9%b4%e4%ba%86%ef%bc%8c%e8%bf%98%e6%9c%89%e4%ba%ba%e5%9c%a8%e5%88%86%e6%9e%90dirty-cow","status":"publish","type":"post","link":"http:\/\/pareto.fun\/?p=606","title":{"rendered":"\u8981\u5341\u5e74\u4e86\uff0c\u8fd8\u6709\u4eba\u5728\u5206\u6790dirty cow"},"content":{"rendered":"\n<p>\u7531MADV_DONTNEED\u5f15\u53d1\u7684\u7ade\u6001\u6761\u4ef6\uff0c \u770b\u5b8c\u4e4b\u540e\u76f4\u547c\u8fc7\u763e \uff0c\u8fd9\u4e2a\u6f0f\u6d1e\u786e\u5b9e\u725b\u903c\u3002<br>\u8d34\u4e00\u4efd\u6f0f\u6d1e\u7ec6\u8282 \uff0chttps:\/\/github.com\/dirtycow\/dirtycow.github.io\/wiki\/VulnerabilityDetails <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>faultin_page\r\n  handle_mm_fault\r\n    __handle_mm_fault\r\n      handle_pte_fault\r\n        do_fault &lt;- pte is not present\r\n\t  do_cow_fault &lt;- FAULT_FLAG_WRITE\r\n\t    alloc_set_pte\r\n\t      maybe_mkwrite(pte_mkdirty(entry), vma) &lt;- mark the page dirty\r\n\t      \t\t\t\t\t\tbut keep it RO \r\n# Returns with 0 and retry\r\nfollow_page_mask\r\n  follow_page_pte\r\n    (flags &amp; FOLL_WRITE) &amp;&amp; !pte_write(pte) &lt;- retry fault\r\nfaultin_page\r\n  handle_mm_fault\r\n    __handle_mm_fault\r\n      handle_pte_fault\r\n        FAULT_FLAG_WRITE &amp;&amp; !pte_write\r\n\t  do_wp_page\r\n\t    PageAnon() &lt;- this is CoWed page already\r\n\t    reuse_swap_page &lt;- page is exclusively ours\r\n\t    wp_page_reuse\r\n\t      maybe_mkwrite &lt;- dirty but RO again\r\n\t      ret = VM_FAULT_WRITE\r\n((ret &amp; VM_FAULT_WRITE) &amp;&amp; !(vma->vm_flags &amp; VM_WRITE)) &lt;- we drop FOLL_WRITE\r\n# Returns with 0 and retry as a read fault\r\ncond_resched -> different thread will now unmap via madvise\r\nfollow_page_mask\r\n  !pte_present &amp;&amp; pte_none\r\nfaultin_page\r\n  handle_mm_fault\r\n    __handle_mm_fault\r\n      handle_pte_fault\r\n        do_fault &lt;- pte is not present\r\n\t  do_read_fault &lt;- this is a read fault and we will get pagecache\r\n\t  \t\t   page!<\/code><\/pre>\n\n\n\n<p>\u60f3\u8981\u641e\u660e\u767d\u6574\u4e2a\u6d41\u7a0b\u662f\u4e2a\u600e\u4e48\u56de\u4e8b\uff0c\u8981\u56f4\u7ed5\u4e00\u4e2a\u4e3b\u9898<\/p>\n\n\n\n<ul>\n<li>COW \u673a\u5236<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">COW \u5b9e\u73b0\u6d41\u7a0b<\/h2>\n\n\n\n<p>\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u4e86\u8282\u7ea6\u5185\u5b58\uff0c\u591a\u4e2a\u8fdb\u7a0b\u5171\u7528\u4f1a\u540c\u4e00\u5757\uff08\u5171\u4eab\u53ef\u8bfb\uff09\u7269\u7406\u5185\u5b58\uff0c\u5f53\u5bf9\u5185\u5b58\u53d1\u751f\u5199\u884c\u4e3a\u65f6\uff0c\u5185\u6838\u624d\u5206\u914d\u4e00\u5757\u7684\u65b0\u7684\u7269\u7406\u5185\u5b58\uff0c\u5e76\u5c06\u4fee\u6539\u5199\u5165\u5230\u8be5\uff08\u79c1\u6709\u53ef\u5199\uff09\u7269\u7406\u5185\u5b58\uff0c\u4e0d\u5f71\u54cd\u5176\u4ed6\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<p>\u4ece\u903b\u8f91\u4e0a\u8bb2\uff0c COW \u673a\u5236\u7531\u5199\u884c\u4e3a\u89e6\u53d1 \uff0c \u5f53\u8c03\u7528\u5199\u5185\u5b58\u65f6\uff0c\u5185\u6838\u7684\u51fd\u6570\u8c03\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>sys_write() => vfs_write() => __vfs_write() => file->f_op->write() => mem_write() => mem_rw() => __access_remote_vm() => get_user_pages_remote() => __get_user_pages_locked() => \uff01\u91cd\u70b9\u51fd\u6570__get_user_pages\uff08\uff09<\/p>\n\n\n\n<p>\u7b80\u5355\u7406\u89e3\u4e0b\u8fd9\u4e2a\u6d41\u7a0b \uff0c\u9664\u4e86\u63a5\u53e3\u7684\u4e00\u5c42\u5c42\u7684\u5c01\u88c5\uff0c\u8981\u5199\u4e00\u5757\u5185\u5b58\u9700\u8981\u7d22\u5f15\u5230\u5185\u5b58\u5730\u5740\uff0c\u63a5\u53e3\u7684\u53c2\u6570\u4e3a\u865a\u62df\u5730\u5740\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u7269\u7406\u5730\u5740\uff0c\u518d\u6267\u884c\u5199\u5165\u3002<\/p>\n\n\n\n<p>COW \u673a\u5236\u5c31\u9690\u85cf\u5728 \u4ece\u865a\u62df\u5730\u5740\u83b7\u53d6\u7269\u7406\u5730\u5740\u7684\u6d41\u7a0b\u4e2d,  __get_user_pages() \u5185\u90e8\u3002<\/p>\n\n\n\n<p>\u5148\u8fdb\u884c\u4e00\u4e9bVMA\u7684\u68c0\u67e5\uff0c\u8c03\u7528follow_page_mask() <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L574\">https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L574<\/a> \u67e5\u627e\u7269\u7406\u9875\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">follow_page_mask \u7b2c\u4e00\u6b21\u8c03\u7528<\/h3>\n\n\n\n<p>\u8fd4\u56deNULL\uff0c \u7531\u4e8e\u8be5\u865a\u62df\u5730\u5740\u5bf9\u5e94\u7684\u7269\u7406\u9875\u9762\u5c1a\u672a\u88ab\u5206\u914d\u6216\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<p>\u8c03\u7528faultin_page => do_cow_fault \u5206\u914d\u5bf9\u5e94\u7684cow_page <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/memory.c#L3048\">\u5206\u914dcow_page<\/a> \uff0c\u5e76\u5c06cow_page \u8bbe\u7f6e\u4e3adirty <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/memory.c#L3092\">https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/memory.c#L3092<\/a>  \u968f\u540efaultin_page\u8fd4\u56de0 \u3002<\/p>\n\n\n\n<p>\u901a\u8fc7goto https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L581 \u518d\u6b21\u8c03\u7528 follow_page_mask \u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">follow_page_mask \u7b2c\u4e8c\u6b21\u8c03\u7528<\/h3>\n\n\n\n<p>\u5bf9\u5e94\u7684pte \u4e3a\u53ea\u8bfbhttps:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L98\uff0c\u518d\u6b21\u8fd4\u56deNULL\u3002 \u8c03\u7528faultin_page => do_wp_fault \u5b8c\u6210\u7269\u7406\u9875\u7684\u590d\u5236 ,faultin_page \u8fd4\u56de0 \u3002\u5e76\u79fb\u9664 FOLL_WRITE<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">follow_page_mask \u7b2c\u4e09\u6b21\u8c03\u7528<\/h3>\n\n\n\n<p>\u7531\u4e8e\u53d6\u6d88\u4e86 FOLL_WRITE \u8868\u793a follow_page_mask\u8fd4\u56de\u5185\u5b58\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p> \uff1f\uff1f<\/p>\n\n\n\n<p> \u8fd9\u91cc\u6709\u4e2a\u7591\u95ee \u4e00\u5757\u533f\u540d\u53ea\u8bfb\u7684\u5185\u5b58\uff0c  COW \u5b8c\u6210\u540e\uff0c \u4e0d\u5e94\u8be5\u8fd4\u56de\u7684\u4e5f\u662f \u79c1\u6709\u53ea\u8bfb\u7684\u5185\u5b58\u5417 \uff0c\u56e0\u4e3amay_mkwrite \u8bbe\u7f6e\u5199\u6743\u9650 \u4f1a\u5224\u65advma->vm_flags \uff0c\u6b64\u65f6\u5fc5\u5b9a\u4e0d\u53ef\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4f2a\u4ee3\u7801\npte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) {\n    if (vma->vm_flags &amp; VM_WRITE) { \/\/ \u68c0\u67e5 VMA \u662f\u5426\u5141\u8bb8\u5199\u5165\n        \/\/ \u5982\u679c\u5141\u8bb8\uff0c\u5219\u5728 PTE \u4e2d\u8bbe\u7f6e\u5199\u6743\u9650\u4f4d\n        pte = pte_mkwrite(pte);\n    }\n    return pte;\n}\n<\/code><\/pre>\n\n\n\n<p>\u54e6 \u5bf9\u5e94\u8fd9\u4e2a\u60c5\u51b5 https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L411 \u53bb\u6389\u4e86FOLL_WRITE<br>\u6240\u4ee5\u80fd\u901a\u8fc7 https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L103 \u627e\u5230<\/p>\n\n\n\n<p>\uff1f\uff1f<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6f0f\u6d1e\u5229\u7528<\/h2>\n\n\n\n<p>\u5173\u952e\u70b91 : drop FOLL_WRITE <\/p>\n\n\n\n<p><a href=\"http:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L411\">http:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L411<\/a> \u79fb\u9664FOLL_WRITE \u6807\u5fd7<\/p>\n\n\n\n<p>\u5173\u952e\u70b92\uff1a\u5229\u7528<strong>madvise_dontneed<\/strong> \u5bfc\u81f4 !pte_present &amp;&amp; pte_none \u6210\u7acb<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>follow_page_mask \u6bcf\u6b21retry\u65f6 \uff0c\u8c03\u7528cond_resched() , \u542f\u7528\u8c03\u5ea6\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5728\u7b2c\u4e8c\u6b21follow_page_mask \uff0c\u901a\u8fc7 madvise_dontneed page \u5c06\u4e0d\u5728\u5185\u5b58\uff0c\u6784\u6210\u6761\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!pte_present &amp;&amp; pte_none <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L87\">https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L87<\/a> <\/code><\/pre>\n\n\n\n<p>\u518d\u6b21\u89e6\u53d1faultin_page \u7531\u4e8e\u53bb\u6389\u4e86FOLL_WRITE <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L365\">https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/gup.c#L365<\/a> <\/p>\n\n\n\n<p>\u5bfc\u81f4 <a href=\"https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/memory.c#L3188\">https:\/\/github.com\/torvalds\/linux\/blob\/v4.7\/mm\/memory.c#L3188<\/a> \u6210\u7acb <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (!(flags &amp; FAULT_FLAG_WRITE))<br>    return do_read_fault(mm, vma, address, pmd, pgoff, flags,<\/pre>\n\n\n\n<p>\u76f4\u63a5\u8fd4\u56de\u4e86\u53ea\u8bfb\u5185\u5b58\u7684page \u5230 \u7528\u6237\u7a7a\u95f4 \uff0c \u5199\u5165\u6570\u636e\u5c06\u5bfc\u81f4dirty \uff0c\u5c06\u4fee\u6539\u5199\u5165\u5230\u5bf9\u5e94\u7684\u6587\u4ef6\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/www.anquanke.com\/post\/id\/84851\">https:\/\/www.anquanke.com\/post\/id\/84851<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/dirtycow\/dirtycow.github.io\/blob\/master\/dirtyc0w.c\">https:\/\/github.com\/dirtycow\/dirtycow.github.io\/blob\/master\/dirtyc0w.c<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/dirtycow\/dirtycow.github.io\/wiki\/VulnerabilityDetails\">https:\/\/github.com\/dirtycow\/dirtycow.github.io\/wiki\/VulnerabilityDetails<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7531MADV_DONTNEED\u5f15\u53d1\u7684\u7ade\u6001\u6761\u4ef6\uff0c \u770b\u5b8c\u4e4b\u540e\u76f4\u547c\u8fc7\u763e \uff0c\u8fd9\u4e2a\u6f0f\u6d1e\u786e\u5b9e\u725b\u903c\u3002\u8d34\u4e00\u4efd\u6f0f\u6d1e\u7ec6\u8282  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,12],"tags":[],"_links":{"self":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/606"}],"collection":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=606"}],"version-history":[{"count":2,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/606\/revisions"}],"predecessor-version":[{"id":608,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/606\/revisions\/608"}],"wp:attachment":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=606"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}