| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Functions corresponding to SET password methods under BIOS attributes interface GUID |
| 4 |
* |
| 5 |
* Copyright (c) 2020 Dell Inc. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/wmi.h> |
| 9 |
#include "dell-wmi-sysman.h" |
| 10 |
|
| 11 |
static int call_password_interface(struct wmi_device *wdev, char *in_args, size_t size) |
| 12 |
{ |
| 13 |
struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 14 |
struct acpi_buffer input; |
| 15 |
union acpi_object *obj; |
| 16 |
acpi_status status; |
| 17 |
int ret = -EIO; |
| 18 |
|
| 19 |
input.length = (acpi_size) size; |
| 20 |
input.pointer = in_args; |
| 21 |
status = wmidev_evaluate_method(wdev, 0, 1, &input, &output); |
| 22 |
if (ACPI_FAILURE(status)) |
| 23 |
return -EIO; |
| 24 |
obj = (union acpi_object *)output.pointer; |
| 25 |
if (obj->type == ACPI_TYPE_INTEGER) |
| 26 |
ret = obj->integer.value; |
| 27 |
|
| 28 |
kfree(output.pointer); |
| 29 |
/* let userland know it may need to check is_password_set again */ |
| 30 |
kobject_uevent(&wmi_priv.class_dev->kobj, KOBJ_CHANGE); |
| 31 |
return map_wmi_error(ret); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* set_new_password() - Sets a system admin password |
| 36 |
* @password_type: The type of password to set |
| 37 |
* @new: The new password |
| 38 |
* |
| 39 |
* Sets the password using plaintext interface |
| 40 |
*/ |
| 41 |
int set_new_password(const char *password_type, const char *new) |
| 42 |
{ |
| 43 |
size_t password_type_size, current_password_size, new_size; |
| 44 |
size_t security_area_size, buffer_size; |
| 45 |
char *buffer = NULL, *start; |
| 46 |
char *current_password; |
| 47 |
int ret; |
| 48 |
|
| 49 |
mutex_lock(&wmi_priv.mutex); |
| 50 |
if (!wmi_priv.password_attr_wdev) { |
| 51 |
ret = -ENODEV; |
| 52 |
goto out; |
| 53 |
} |
| 54 |
if (strcmp(password_type, "Admin") == 0) { |
| 55 |
current_password = wmi_priv.current_admin_password; |
| 56 |
} else if (strcmp(password_type, "System") == 0) { |
| 57 |
current_password = wmi_priv.current_system_password; |
| 58 |
} else { |
| 59 |
ret = -EINVAL; |
| 60 |
dev_err(&wmi_priv.password_attr_wdev->dev, "unknown password type %s\n", |
| 61 |
password_type); |
| 62 |
goto out; |
| 63 |
} |
| 64 |
|
| 65 |
/* build/calculate buffer */ |
| 66 |
security_area_size = calculate_security_buffer(wmi_priv.current_admin_password); |
| 67 |
password_type_size = calculate_string_buffer(password_type); |
| 68 |
current_password_size = calculate_string_buffer(current_password); |
| 69 |
new_size = calculate_string_buffer(new); |
| 70 |
buffer_size = security_area_size + password_type_size + current_password_size + new_size; |
| 71 |
buffer = kzalloc(buffer_size, GFP_KERNEL); |
| 72 |
if (!buffer) { |
| 73 |
ret = -ENOMEM; |
| 74 |
goto out; |
| 75 |
} |
| 76 |
|
| 77 |
/* build security area */ |
| 78 |
populate_security_buffer(buffer, wmi_priv.current_admin_password); |
| 79 |
|
| 80 |
/* build variables to set */ |
| 81 |
start = buffer + security_area_size; |
| 82 |
ret = populate_string_buffer(start, password_type_size, password_type); |
| 83 |
if (ret < 0) |
| 84 |
goto out; |
| 85 |
|
| 86 |
start += ret; |
| 87 |
ret = populate_string_buffer(start, current_password_size, current_password); |
| 88 |
if (ret < 0) |
| 89 |
goto out; |
| 90 |
|
| 91 |
start += ret; |
| 92 |
ret = populate_string_buffer(start, new_size, new); |
| 93 |
if (ret < 0) |
| 94 |
goto out; |
| 95 |
|
| 96 |
ret = call_password_interface(wmi_priv.password_attr_wdev, buffer, buffer_size); |
| 97 |
/* on success copy the new password to current password */ |
| 98 |
if (!ret) |
| 99 |
strscpy(current_password, new, MAX_BUFF); |
| 100 |
/* explain to user the detailed failure reason */ |
| 101 |
else if (ret == -EOPNOTSUPP) |
| 102 |
dev_err(&wmi_priv.password_attr_wdev->dev, "admin password must be configured\n"); |
| 103 |
else if (ret == -EACCES) |
| 104 |
dev_err(&wmi_priv.password_attr_wdev->dev, "invalid password\n"); |
| 105 |
|
| 106 |
out: |
| 107 |
kfree(buffer); |
| 108 |
mutex_unlock(&wmi_priv.mutex); |
| 109 |
|
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
static int bios_attr_pass_interface_probe(struct wmi_device *wdev, const void *context) |
| 114 |
{ |
| 115 |
mutex_lock(&wmi_priv.mutex); |
| 116 |
wmi_priv.password_attr_wdev = wdev; |
| 117 |
mutex_unlock(&wmi_priv.mutex); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void bios_attr_pass_interface_remove(struct wmi_device *wdev) |
| 122 |
{ |
| 123 |
mutex_lock(&wmi_priv.mutex); |
| 124 |
wmi_priv.password_attr_wdev = NULL; |
| 125 |
mutex_unlock(&wmi_priv.mutex); |
| 126 |
} |
| 127 |
|
| 128 |
static const struct wmi_device_id bios_attr_pass_interface_id_table[] = { |
| 129 |
{ .guid_string = DELL_WMI_BIOS_PASSWORD_INTERFACE_GUID }, |
| 130 |
{ }, |
| 131 |
}; |
| 132 |
static struct wmi_driver bios_attr_pass_interface_driver = { |
| 133 |
.driver = { |
| 134 |
.name = DRIVER_NAME"-password" |
| 135 |
}, |
| 136 |
.probe = bios_attr_pass_interface_probe, |
| 137 |
.remove = bios_attr_pass_interface_remove, |
| 138 |
.id_table = bios_attr_pass_interface_id_table, |
| 139 |
}; |
| 140 |
|
| 141 |
int init_bios_attr_pass_interface(void) |
| 142 |
{ |
| 143 |
return wmi_driver_register(&bios_attr_pass_interface_driver); |
| 144 |
} |
| 145 |
|
| 146 |
void exit_bios_attr_pass_interface(void) |
| 147 |
{ |
| 148 |
wmi_driver_unregister(&bios_attr_pass_interface_driver); |
| 149 |
} |
| 150 |
|
| 151 |
MODULE_DEVICE_TABLE(wmi, bios_attr_pass_interface_id_table); |