{"id":100415,"date":"2018-12-06T07:00:00","date_gmt":"2018-12-06T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100415"},"modified":"2019-03-13T00:10:52","modified_gmt":"2019-03-13T07:10:52","slug":"20181206-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20181206-00\/?p=100415","title":{"rendered":"How is it that WriteProcessMemory succeeds in writing to read-only memory?"},"content":{"rendered":"<p>When you call <code>Write&shy;Process&shy;Memory<\/code> and tell it to write to memory that is read-only, the <code>Write&shy;Process&shy;Memory<\/code> succeeds. How can that be? <\/p>\n<p>Because <code>Write&shy;Process&shy;Memory<\/code> tries really hard to please you. <\/p>\n<p>As I noted some time ago, <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20120808-00\/?p=6913\">the primary audience for functions like <code>Create&shy;Remote&shy;Thread<\/code> and <code>Write&shy;Process&shy;Memory<\/code> is debuggers<\/a>. And when debuggers try to patch memory, it&#8217;s often for things like patching in a breakpoint instruction or doing some <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/debugger\/edit-and-continue?view=vs-2015\">edit-and-continue<\/a> magic. So the <code>Write&shy;Process&shy;Memory<\/code> tries really hard to get those bytes written. If the page is read-only, <code>Write&shy;Process&shy;Memory<\/code> temporarily changes the permission to read-write, updates the memory, and then restores the original permission. <\/p>\n<p>&#8220;No need to thank me, just trying to help.&#8221; <\/p>\n<p>There is a race condition if the target process happens to be manipulating the page protection at the same time that <code>Write&shy;Process&shy;Memory<\/code> is. But that&#8217;s okay, because the intended audience is debuggers, and debuggers will freeze the target process before trying to edit its memory. <\/p>\n<p>There is no security hole here, because the way the <code>Write&shy;Process&shy;Memory<\/code> function changes the page protection is basically <code>Virtual&shy;Protect&shy;Ex<\/code>, so it will succeed only if you already could have modified the protections yourself anyway. If you didn&#8217;t have permission to change the protections, then <code>Write&shy;Process&shy;Memory<\/code>&#8216;s attempt to change the protections would fail too. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because it tries really hard.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because it tries really hard.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=100415"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100415\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=100415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}