Skip to content

Instantly share code, notes, and snippets.

@Jongy
Created January 2, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jongy/72669e548fa5b43c370ede92ed56e544 to your computer and use it in GitHub Desktop.
Save Jongy/72669e548fa5b43c370ede92ed56e544 to your computer and use it in GitHub Desktop.
Linux kernel MicroPython snippet "disabling" /dev/urandom for specific commands.
from kernel_ffi import callback, current, str as s
task_struct = partial_struct("task_struct")
file_operations = partial_struct("file_operations")
real_urandom_read = urandom_read
no_random_progs = ["ssh-keygen"]
def my_urandom_read(filp, buf, count, ppos):
# technically should take task lock for 'comm' but meh.
if s(int(task_struct(current()).comm)) in no_random_progs:
print("no random for you!")
# lousy copy_to_user, should be improved if you use with untrusted programs
memcpy(buf, b"a" * count, count)
return count
return real_urandom_read(filp, buf, count, ppos)
cb = callback(my_urandom_read)
file_operations(urandom_fops).read = cb.ptr()
# to stop
file_operations(urandom_fops).read = int(real_urandom_read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment