Created
January 2, 2020 17:27
-
-
Save Jongy/72669e548fa5b43c370ede92ed56e544 to your computer and use it in GitHub Desktop.
Linux kernel MicroPython snippet "disabling" /dev/urandom for specific commands.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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