title: read/write process memory when sudo has no permission to do so
body: <p>I have a short python script, which is reading memory of other process. When I am going through the maps, I can see that some pieces of the memory don't give me read/write permissions. That is true even when I run the script with sudo. Is there any way to get these permissions? I am using newest Ubuntu and code in python3. The code I use is shown below.</p>
  <pre><code>import re
  import sys
  
  def search(number,chunk):
      for bit in chunk:
          if bit == number:
              print(&quot;Hit!: &quot; + str(number))
              
  def search16bits(number, chunk):
      length = len(chunk)/8
      #print(&quot;Length: &quot; + str(length))
      num = int(length/2)
      #print(chunk[int(num/8)-1])
      #for i in range(num):
          #print(i)
      for i in range(num - 1):
          start = num
          second = start + 1
          result = chunk[second] + 256 * chunk[start]
          #print(result)
          if number == result:
              print(&quot;Hit!: &quot; + str(number))
              exit()
          
  
  def print_memory_of_pid(pid, only_writable=True):
      &quot;&quot;&quot; 
      Run as root, take an integer PID and return the contents of memory to STDOUT
      &quot;&quot;&quot;
      memory_permissions = 'rw' if only_writable else 'r-'
      sys.stderr.write(&quot;PID = %d&quot; % pid)
      with open(&quot;/proc/%d/maps&quot; % pid, 'r') as maps_file:
          with open(&quot;/proc/%d/mem&quot; % pid, 'rb') as mem_file:
              for line in maps_file.readlines():  # for each mapped region
                  m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r][-w])', line)
                  #print(&quot;m:&quot; + str(m))
                  if m.group(3) == memory_permissions: 
                      #sys.stderr.write(&quot;\nOK : \n&quot; + line+&quot;\n&quot;)
                      start = int(m.group(1), 16)
                      if start &gt; 0xFFFFFFFFFFFF:
                          continue
                      end = int(m.group(2), 16)
                      sys.stderr.write( &quot;start = &quot; + str(start) + &quot;\n&quot;)
                      mem_file.seek(start)  # seek to region start
                      chunk = mem_file.read(end - start)  # read region contents
                      #print()
                      #print()
                      print(&quot;###########################Next Chunk:######################################&quot;)
                      #search16bits(221, chunk)
                      #print(len(chunk))
                      #print(chunk)  # dump contents to standard output
                  else:
                      print(&quot;No permission&quot;)
                      #sys.stderr.write(&quot;\nPASS : \n&quot; + line+&quot;\n&quot;)
  
  if __name__ == '__main__': # Execute this code when run from the commandline.
      try:
          assert len(sys.argv) == 2, &quot;Provide exactly 1 PID (process ID)&quot;
          pid = int(sys.argv[1])
          print_memory_of_pid(pid)
      except (AssertionError, ValueError) as e:
          print(&quot;Please provide 1 PID as a commandline argument.&quot;)
          print(&quot;You entered: %s&quot; + str(sys.argv))
          raise e
  </code></pre>