Skip to content

Commit

Permalink
corrected size in block_devices on darwin, linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwa Shah committed Aug 7, 2017
1 parent b4316a5 commit 7078819
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion osquery/core/darwin/conversions.cpp
Expand Up @@ -65,7 +65,7 @@ std::string stringFromCFData(const CFDataRef& cf_data) {
}

std::string stringFromCFNumber(const CFDataRef& cf_number) {
return stringFromCFNumber(cf_number, kCFNumberIntType);
return stringFromCFNumber(cf_number, CFNumberGetType((CFNumberRef)cf_number));
}

std::string stringFromCFNumber(const CFDataRef& cf_number, CFNumberType type) {
Expand Down
19 changes: 19 additions & 0 deletions osquery/events/darwin/iokit.cpp
Expand Up @@ -90,6 +90,25 @@ std::string getIOKitProperty(const CFMutableDictionaryRef& details,
return value;
}

long long int getNumIOKitProperty(const CFMutableDictionaryRef& details,
const std::string& key) {
// Get a property from the device.
auto cfkey = CFStringCreateWithCString(
kCFAllocatorDefault, key.c_str(), kCFStringEncodingUTF8);
auto property = CFDictionaryGetValue(details, cfkey);
CFRelease(cfkey);

// Several supported ways of parsing IOKit-encoded data.
if (property && CFGetTypeID(property) == CFNumberGetTypeID()) {
CFNumberType type = CFNumberGetType((CFNumberRef)property);
long long int value;
CFNumberGetValue((CFNumberRef)property, type, &value);
return value;
}

return 0;
}

void IOKitEventPublisher::restart() {
static std::vector<const std::string*> device_classes = {
&kIOUSBDeviceClassName_,
Expand Down
2 changes: 2 additions & 0 deletions osquery/events/darwin/iokit.h
Expand Up @@ -67,6 +67,8 @@ struct IOKitPCIProperties {

std::string getIOKitProperty(const CFMutableDictionaryRef& details,
const std::string& key);
long long int getNumIOKitProperty(const CFMutableDictionaryRef& details,
const std::string& key);

inline void idToHex(std::string& id) {
long base = 0;
Expand Down
6 changes: 4 additions & 2 deletions osquery/tables/system/darwin/block_devices.cpp
Expand Up @@ -35,8 +35,10 @@ void genIOMediaDevice(const io_service_t& device,

r["uuid"] = getIOKitProperty(properties, "UUID");
r["name"] = "/dev/" + getIOKitProperty(properties, "BSD Name");
r["size"] = getIOKitProperty(properties, "Size");

r["block_size"] = getIOKitProperty(properties, "Preferred Block Size");
auto disk_size = getNumIOKitProperty(properties, "Size");
auto block_size = getNumIOKitProperty(properties, "Preferred Block Size");
r["size"] = boost::lexical_cast<std::string>(disk_size/block_size);
auto type = getIOKitProperty(properties, "Whole");
if (type == "1") {
// The "Whole" property applies to the entire disk entry, not partitions.
Expand Down
5 changes: 5 additions & 0 deletions osquery/tables/system/linux/block_devices.cpp
Expand Up @@ -44,6 +44,11 @@ static void getBlockDevice(struct udev_device *dev, QueryData &results) {
r["size"] = size;
}

const char *block_size = udev_device_get_sysattr_value(dev, "queue/logical_block_size");
if (block_size != nullptr) {
r["block_size"] = block_size;
}

subdev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", nullptr);
if (subdev != nullptr) {
const char *model = udev_device_get_sysattr_value(subdev, "model");
Expand Down
3 changes: 2 additions & 1 deletion specs/posix/block_devices.table
Expand Up @@ -5,7 +5,8 @@ schema([
Column("parent", TEXT, "Block device parent name"),
Column("vendor", TEXT, "Block device vendor string"),
Column("model", TEXT, "Block device model string identifier"),
Column("size", BIGINT, "Block device size in bytes"),
Column("size", BIGINT, "Block device size in blocks"),
Column("block_size", INTEGER, "Block size in bytes"),
Column("uuid", TEXT, "Block device Universally Unique Identifier"),
Column("type", TEXT, "Block device type string"),
Column("label", TEXT, "Block device label string"),
Expand Down

0 comments on commit 7078819

Please sign in to comment.