|
| 1 | +/* Copyright (C) 2025 NooBaa */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const util = require('util'); |
| 6 | +const chance = require('chance')(); |
| 7 | +const child_process = require('child_process'); |
| 8 | +const config = require('../../config'); |
| 9 | +const blockutils = require('linux-blockutils'); |
| 10 | + |
| 11 | +const DEVICE_TYPE_DISK = 'disk'; |
| 12 | + |
| 13 | +const async_delay = util.promisify(setTimeout); |
| 14 | +const async_exec = util.promisify(child_process.exec); |
| 15 | +const get_block_info_async = util.promisify(blockutils.getBlockInfo); |
| 16 | + |
| 17 | +/** |
| 18 | + * generate_entropy will create randomness by changing the MD5 |
| 19 | + * using information from the device (disk) |
| 20 | + * it will run as long as the callback it true |
| 21 | + * @param {() => Boolean} loop_cond |
| 22 | + */ |
| 23 | +async function generate_entropy(loop_cond) { |
| 24 | + if (process.platform !== 'linux' || process.env.container === 'docker') return; |
| 25 | + while (loop_cond()) { |
| 26 | + try { |
| 27 | + await async_delay(1000); |
| 28 | + |
| 29 | + // most likely we will read from the disk for no reason at all as the caller |
| 30 | + // already finished initializing its randomness |
| 31 | + if (loop_cond()) break; |
| 32 | + |
| 33 | + const ENTROPY_AVAIL_PATH = '/proc/sys/kernel/random/entropy_avail'; |
| 34 | + const entropy_avail = parseInt(await fs.promises.readFile(ENTROPY_AVAIL_PATH, 'utf8'), 10); |
| 35 | + console.log(`generate_entropy: entropy_avail ${entropy_avail}`); |
| 36 | + if (entropy_avail >= config.ENTROPY_MIN_THRESHOLD) return; |
| 37 | + const available_disks = await get_block_device_disk_info(); |
| 38 | + const disk_details = pick_a_disk(available_disks); |
| 39 | + if (disk_details) { |
| 40 | + await generate_entropy_from_disk(disk_details.name, disk_details.size); |
| 41 | + } else { |
| 42 | + throw new Error('No disk candidates found'); |
| 43 | + } |
| 44 | + } catch (err) { |
| 45 | + // we intentionally don't print the error |
| 46 | + // as console.err/warm and the error with stack trace |
| 47 | + // as the generate_entropy is our best effort to generate entropy |
| 48 | + // until Linux will finally do it |
| 49 | + console.log('generate_entropy: retrying'); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * get_block_device_disk_info |
| 56 | + * (on Linux) will return array of disk devices |
| 57 | + * that their names starts with /dev/ |
| 58 | + * and their size it a number in bytes |
| 59 | + * (else) will return an empty array |
| 60 | + * Note: under the hood it uses blockutils.getBlockInfo which calls lsblk command |
| 61 | + * we used the option onlyStandard for parsing "disk" and "part" entries |
| 62 | + * reference: https://github.com/mw-white/node-linux-blockutils |
| 63 | + * @returns {Promise<object[]>} |
| 64 | + */ |
| 65 | +async function get_block_device_disk_info() { |
| 66 | + const is_linux = process.platform === 'linux'; |
| 67 | + if (!is_linux) return []; |
| 68 | + |
| 69 | + const block_devices = await get_block_info_async({ onlyStandard: true }); |
| 70 | + if (!block_devices) return []; |
| 71 | + |
| 72 | + const available_disks = []; |
| 73 | + for (const block_device of block_devices) { |
| 74 | + if (block_device.TYPE === DEVICE_TYPE_DISK) { |
| 75 | + available_disks.push({ |
| 76 | + name: `/dev/${block_device.NAME}`, |
| 77 | + size: Number(block_device.SIZE), |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + return available_disks; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * pick_a_disk will pick a disk that: |
| 86 | + * - The disk name is in the disk_order_by_priority array - the lower index in the array the higher priority |
| 87 | + * - Its size is higher than config.ENTROPY_DISK_SIZE_THRESHOLD |
| 88 | + * |
| 89 | + * The item that is returned is an object with properties: name, size and priority |
| 90 | + * (we could delete the priority property as it is used only inside this function) |
| 91 | + * |
| 92 | + * if none matches then it would return undefined |
| 93 | + * @param {object[]} available_disks |
| 94 | + * @returns {object | undefined} |
| 95 | + */ |
| 96 | +function pick_a_disk(available_disks) { |
| 97 | + // the disk_order_by_priority array is the whitelist and also sets the priority |
| 98 | + // (0 index highest priority, n-1 index lowest priority) |
| 99 | + // note: we decided on the order according to previous implantation we had in the past |
| 100 | + // as it added in patches we might want to reconsider it |
| 101 | + const disk_order_by_priority = ['/dev/sd', '/dev/vd', '/dev/xvd', 'dev/dasd', '/dev/nvme']; |
| 102 | + const priority_disk_array = []; |
| 103 | + for (const disk of available_disks) { |
| 104 | + if (disk.size > config.ENTROPY_DISK_SIZE_THRESHOLD) { |
| 105 | + for (let index = 0; index < disk_order_by_priority.length; index++) { |
| 106 | + const prefix = disk_order_by_priority[index]; |
| 107 | + if (disk.name.startsWith(prefix)) { |
| 108 | + disk.priority = index; |
| 109 | + priority_disk_array.push(disk); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + // sort the array based on the priority |
| 115 | + const sorted_priority_disk_array = priority_disk_array.sort((item1, item2) => item1.priority - item2.priority); |
| 116 | + const first_element = sorted_priority_disk_array.length > 0 ? sorted_priority_disk_array[0] : undefined; |
| 117 | + return first_element; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * generate_entropy_from_disk will execute dd command which pipes to md5sum |
| 122 | + * in order to get the MD5 hash to change |
| 123 | + * @param {string} disk_name |
| 124 | + * @param {number} disk_size |
| 125 | + */ |
| 126 | +async function generate_entropy_from_disk(disk_name, disk_size) { |
| 127 | + const bs = 1024 * 1024; |
| 128 | + const count = 32; |
| 129 | + const disk_size_in_blocks = disk_size / bs; |
| 130 | + const skip = chance.integer({ min: 0, max: disk_size_in_blocks - 1}); // reduce 1 from the max because the range is inclusive in chance.integer() |
| 131 | + console.log(`generate_entropy_from_disk: adding entropy: dd if=${disk_name} bs=${bs} count=${count} skip=${skip} | md5sum`); |
| 132 | + await async_exec(`dd if=${disk_name} bs=${bs} count=${count} skip=${skip} | md5sum`); |
| 133 | +} |
| 134 | + |
| 135 | +exports.generate_entropy = generate_entropy; |
| 136 | +exports.get_block_device_disk_info = get_block_device_disk_info; |
| 137 | +exports.pick_a_disk = pick_a_disk; |
| 138 | +exports.generate_entropy_from_disk = generate_entropy_from_disk; |
0 commit comments