import json
import subprocess
import sys
def check_json_value(json_data):
for key, value in json_data.items():
if not isinstance(value, (bool, str, list)):
return False
if isinstance(value, list):
if not all(isinstance(item, str) for item in value):
return False
return True
def check_json_content(json_data):
if len(json_data) == 1 and "kernelpermission" in json_data:
json_data = json_data["kernelpermission"]
return check_json_value(json_data)
else:
return False
def check_json_file(file_path):
try:
with open(file_path, 'r') as file:
json_data = json.load(file)
if check_json_content(json_data):
return True
else:
print("kernel_permission.json is invalid")
return False
except FileNotFoundError:
print("kernel_permission.json not found")
return False
except json.JSONDecodeError:
print("kernel_permission.json doesn't conform to json specification")
return False
except Exception:
print("kernel_permission.json not found")
return False
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python script.py /path/to/library.so")
sys.exit(1)
so_path = sys.argv[1]
if (check_json_file("kernel_permission.json")):
command = ['llvm-objcopy', '--add-section', '.kernelpermission=kernel_permission.json', so_path]
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
print("llvm-objcopy executed successfully")
else:
print("llvm-objcopy failed")
print("Error:", result.stderr)