Advertisement
xosski

Binary research tool

Jan 9th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/fs.h>
  6. #include <linux/slab.h>
  7. #include <linux/dm-io.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/limits.h>
  12.  
  13. struct TreeNode {
  14. int data;
  15. struct TreeNode *left;
  16. struct TreeNode *right;
  17. };
  18.  
  19. // Function to free the entire tree
  20. void free_tree(struct TreeNode *node) {
  21. if (node == NULL)
  22. return;
  23.  
  24. free_tree(node->left);
  25. free_tree(node->right);
  26. kfree(node);
  27. }
  28.  
  29. // Function to read a sector from the block device
  30. int read_sector(struct block_device *bdev, void *buffer, unsigned long sector, unsigned int count) {
  31. struct dm_io_region io_region;
  32. struct dm_io_request io_req;
  33. struct dm_io_client *io_client;
  34. int ret;
  35.  
  36. io_client = dm_io_client_create();
  37. if (IS_ERR(io_client)) {
  38. pr_err("Failed to create I/O client\n");
  39. return PTR_ERR(io_client);
  40. }
  41.  
  42. io_region.bdev = bdev;
  43. io_region.sector = sector;
  44. io_region.count = count;
  45.  
  46. io_req.bi_op = REQ_OP_READ;
  47. io_req.bi_op_flags = 0;
  48. io_req.mem.type = DM_IO_KMEM;
  49. io_req.mem.ptr.addr = buffer;
  50. io_req.client = io_client;
  51. io_req.notify.fn = NULL;
  52. io_req.notify.context = NULL;
  53.  
  54. ret = dm_io(&io_req, 1, &io_region, NULL);
  55. if (ret) {
  56. pr_err("I/O read failed\n");
  57. }
  58.  
  59. dm_io_client_destroy(io_client);
  60. return ret;
  61. }
  62.  
  63. // Function to find the largest number in the tree
  64. int find_largest_number(struct TreeNode *root) {
  65. int left_max, right_max, max_value;
  66.  
  67. if (root == NULL)
  68. return INT_MIN;
  69.  
  70. left_max = find_largest_number(root->left);
  71. right_max = find_largest_number(root->right);
  72.  
  73. max_value = root->data;
  74. if (left_max > max_value)
  75. max_value = left_max;
  76. if (right_max > max_value)
  77. max_value = right_max;
  78.  
  79. return max_value;
  80. }
  81.  
  82. // Function to recursively reconstruct the binary tree
  83. int reconstruct_tree(struct block_device *bdev, unsigned long *sector, struct TreeNode **node) {
  84. void *buffer;
  85. int ret = 0;
  86. int data;
  87. unsigned long current_sector;
  88.  
  89. // Allocate buffer for reading a sector
  90. buffer = kmalloc(512, GFP_KERNEL); // Assuming sector size is 512 bytes
  91. if (!buffer) {
  92. pr_err("Failed to allocate buffer\n");
  93. return -ENOMEM;
  94. }
  95.  
  96. current_sector = *sector;
  97.  
  98. // Read the current sector
  99. ret = read_sector(bdev, buffer, current_sector, 1);
  100. if (ret) {
  101. kfree(buffer);
  102. return ret;
  103. }
  104.  
  105. // Allocate TreeNode
  106. *node = kmalloc(sizeof(struct TreeNode), GFP_KERNEL);
  107. if (!*node) {
  108. kfree(buffer);
  109. pr_err("Failed to allocate TreeNode\n");
  110. return -ENOMEM;
  111. }
  112.  
  113. data = *((int *)buffer); // The first 4 bytes of the buffer are the data of the node
  114. (*node)->data = data;
  115. (*node)->left = NULL;
  116. (*node)->right = NULL;
  117.  
  118. // Move to the next sector
  119. *sector = current_sector + 1;
  120.  
  121. kfree(buffer);
  122.  
  123. // Recursively reconstruct left subtree
  124. if (*sector % 2 == 0) {
  125. ret = reconstruct_tree(bdev, sector, &(*node)->left);
  126. if (ret) {
  127. free_tree((*node)->left);
  128. kfree(*node);
  129. *node = NULL;
  130. return ret;
  131. }
  132. }
  133.  
  134. // Recursively reconstruct right subtree
  135. if (*sector % 3 == 0) {
  136. ret = reconstruct_tree(bdev, sector, &(*node)->right);
  137. if (ret) {
  138. free_tree((*node)->left);
  139. free_tree((*node)->right);
  140. kfree(*node);
  141. *node = NULL;
  142. return ret;
  143. }
  144. }
  145.  
  146. return 0;
  147. }
  148.  
  149. static int __init module_init_function(void) {
  150. struct block_device *bdev;
  151. struct TreeNode *root = NULL;
  152. int largest_number;
  153. int ret;
  154. unsigned long sector = 0; // Start from the first sector
  155.  
  156. // Open the block device for reading
  157. bdev = blkdev_get_by_path("/dev/sdb", FMODE_READ | FMODE_EXCL, NULL);
  158. if (IS_ERR(bdev)) {
  159. pr_err("Failed to open block device\n");
  160. return PTR_ERR(bdev);
  161. }
  162.  
  163. // Reconstruct the tree from the block device
  164. ret = reconstruct_tree(bdev, &sector, &root);
  165. if (ret) {
  166. pr_err("Failed to reconstruct tree\n");
  167. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  168. return ret;
  169. }
  170.  
  171. // Find the largest number in the tree
  172. largest_number = find_largest_number(root);
  173. pr_info("Largest number in the tree: %d\n", largest_number);
  174.  
  175. // Clean up
  176. free_tree(root); // Free the allocated tree
  177. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  178. return 0;
  179. }
  180.  
  181. static void __exit module_exit_function(void) {
  182. pr_info("Kernel module exited\n");
  183. }
  184.  
  185. module_init(module_init_function);
  186. module_exit(module_exit_function);
  187.  
  188. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement