acpi_battery: use user_strlcpy in acpi_battery_read().

* also check for user addresses in acpi_battery_control().
This commit is contained in:
Jérôme Duval
2018-02-28 20:10:34 +01:00
parent 6470e36518
commit 959fdbd314
3 changed files with 34 additions and 15 deletions
@@ -1,6 +1,6 @@
SubDir HAIKU_TOP src add-ons kernel drivers power acpi_battery ;
UsePrivateHeaders kernel ;
UsePrivateKernelHeaders ;
KernelAddon acpi_battery :
acpi_battery.cpp
@@ -17,6 +17,8 @@
#include <stdlib.h>
#include <string.h>
#include <kernel.h>
#include "device/power_managment.h"
@@ -321,9 +323,9 @@ acpi_battery_read(void* _cookie, off_t position, void *buffer, size_t* numBytes)
ReadBatteryInfo(device->driver_cookie, &batteryInfo);
if (position == 0) {
size_t max_len = *numBytes;
char *str = (char *)buffer;
char string[512];
char *str = string;
size_t max_len = sizeof(string);
snprintf(str, max_len, "Battery Status:\n");
max_len -= strlen(str);
str += strlen(str);
@@ -357,7 +359,10 @@ acpi_battery_read(void* _cookie, off_t position, void *buffer, size_t* numBytes)
max_len -= strlen(str);
str += strlen(str);
*numBytes = strlen((char *)buffer);
max_len = user_strlcpy((char*)buffer, string, *numBytes);
if (max_len < B_OK)
return B_BAD_ADDRESS;
*numBytes = max_len;
} else
*numBytes = 0;
@@ -384,7 +389,11 @@ acpi_battery_control(void* _cookie, uint32 op, void* arg, size_t len)
return B_BAD_VALUE;
uint32 magicId = kMagicACPIBatteryID;
return user_memcpy(arg, &magicId, sizeof(magicId));
if (!IS_USER_ADDRESS(arg)
|| user_memcpy(arg, &magicId, sizeof(magicId)) < B_OK) {
return B_BAD_ADDRESS;
}
return B_OK;
}
case GET_BATTERY_INFO: {
@@ -395,7 +404,12 @@ acpi_battery_control(void* _cookie, uint32 op, void* arg, size_t len)
err = ReadBatteryStatus(device->driver_cookie, &batteryInfo);
if (err != B_OK)
return err;
return user_memcpy(arg, &batteryInfo, sizeof(batteryInfo));
if (!IS_USER_ADDRESS(arg)
|| user_memcpy(arg, &batteryInfo, sizeof(batteryInfo))
< B_OK) {
return B_BAD_ADDRESS;
}
return B_OK;
}
case GET_EXTENDED_BATTERY_INFO: {
@@ -406,7 +420,12 @@ acpi_battery_control(void* _cookie, uint32 op, void* arg, size_t len)
err = ReadBatteryInfo(device->driver_cookie, &extBatteryInfo);
if (err != B_OK)
return err;
return user_memcpy(arg, &extBatteryInfo, sizeof(extBatteryInfo));
if (!IS_USER_ADDRESS(arg)
|| user_memcpy(arg, &extBatteryInfo, sizeof(extBatteryInfo))
< B_OK) {
return B_BAD_ADDRESS;
}
return B_OK;
}
case WATCH_BATTERY:
@@ -210,7 +210,7 @@ acpi_button_select(void *_cookie, uint8 event, selectsync *sync)
status_t error = add_select_sync_pool_entry(&device->select_pool, sync,
event);
if (error != B_OK) {
ERROR("add_select_sync_pool_entry() failed: %" B_PRId32 "\n", error);
ERROR("add_select_sync_pool_entry() failed: %" B_PRIx32 "\n", error);
return error;
}