xref: /linux/drivers/platform/x86/hp/hp-bioscfg/passwdobj-attributes.c (revision 0c22178524864af85ea380be4b2d36078c0edc9c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions corresponding to password object type attributes under
4  * BIOS PASSWORD for use with hp-bioscfg driver.
5  *
6  * Copyright (c) 2022 HP Development Company, L.P.
7  */
8 
9 #include "bioscfg.h"
10 
11 GET_INSTANCE_ID(password);
12 /*
13  * Clear all passwords copied to memory for a particular
14  * authentication instance
15  */
16 static int clear_passwords(const int instance)
17 {
18 	struct password_data *password_data = &bioscfg_drv.password_data[instance];
19 
20 	if (!password_data->is_enabled)
21 		return 0;
22 
23 	memset(password_data->current_password,
24 	       0, sizeof(password_data->current_password));
25 	memset(password_data->new_password,
26 	       0, sizeof(password_data->new_password));
27 
28 	return 0;
29 }
30 
31 /*
32  * Clear all credentials copied to memory for both Power-ON and Setup
33  * BIOS instances
34  */
35 int hp_clear_all_credentials(void)
36 {
37 	int count = bioscfg_drv.password_instances_count;
38 	int instance;
39 
40 	/* clear all passwords */
41 	for (instance = 0; instance < count; instance++)
42 		clear_passwords(instance);
43 
44 	/* clear auth_token */
45 	kfree(bioscfg_drv.spm_data.auth_token);
46 	bioscfg_drv.spm_data.auth_token = NULL;
47 
48 	return 0;
49 }
50 
51 int hp_get_password_instance_for_type(const char *name)
52 {
53 	int count = bioscfg_drv.password_instances_count;
54 	int instance;
55 
56 	for (instance = 0; instance < count; instance++)
57 		if (!strcmp(bioscfg_drv.password_data[instance].common.display_name, name))
58 			return instance;
59 
60 	return -EINVAL;
61 }
62 
63 static int validate_password_input(int instance_id, const char *buf,
64 				   bool is_current)
65 {
66 	int length;
67 	struct password_data *password_data = &bioscfg_drv.password_data[instance_id];
68 
69 	length = strlen(buf);
70 	if (length > 0 && buf[length - 1] == '\n')
71 		length--;
72 
73 	/* An empty current_password clears the session password */
74 	if (is_current && !length)
75 		return 0;
76 
77 	if (length >= MAX_PASSWD_SIZE)
78 		return -E2BIG;
79 
80 	if (password_data->min_password_length > length ||
81 	    password_data->max_password_length < length)
82 		return -E2BIG;
83 
84 	return 0;
85 }
86 
87 ATTRIBUTE_N_PROPERTY_SHOW(is_enabled, password);
88 static struct kobj_attribute password_is_password_set = __ATTR_RO(is_enabled);
89 
90 static int store_password_instance(struct kobject *kobj, const char *buf,
91 				   size_t count, bool is_current)
92 {
93 	char *buf_cp;
94 	int id, ret = 0;
95 
96 	buf_cp = kstrdup(buf, GFP_KERNEL);
97 	if (!buf_cp)
98 		return -ENOMEM;
99 
100 	ret = hp_enforce_single_line_input(buf_cp, count);
101 	if (!ret) {
102 		id = get_password_instance_id(kobj);
103 
104 		if (id >= 0)
105 			ret = validate_password_input(id, buf_cp, is_current);
106 	}
107 
108 	if (!ret) {
109 		if (is_current)
110 			strscpy(bioscfg_drv.password_data[id].current_password, buf_cp);
111 		else
112 			strscpy(bioscfg_drv.password_data[id].new_password, buf_cp);
113 	}
114 
115 	kfree(buf_cp);
116 	return ret < 0 ? ret : count;
117 }
118 
119 static ssize_t current_password_store(struct kobject *kobj,
120 				      struct kobj_attribute *attr,
121 				      const char *buf, size_t count)
122 {
123 	return store_password_instance(kobj, buf, count, true);
124 }
125 
126 static struct kobj_attribute password_current_password = __ATTR_WO(current_password);
127 
128 static ssize_t new_password_store(struct kobject *kobj,
129 				  struct kobj_attribute *attr,
130 				  const char *buf, size_t count)
131 {
132 	return store_password_instance(kobj, buf, count, false);
133 }
134 
135 static struct kobj_attribute password_new_password = __ATTR_WO(new_password);
136 
137 ATTRIBUTE_N_PROPERTY_SHOW(min_password_length, password);
138 static struct kobj_attribute password_min_password_length = __ATTR_RO(min_password_length);
139 
140 ATTRIBUTE_N_PROPERTY_SHOW(max_password_length, password);
141 static struct kobj_attribute password_max_password_length = __ATTR_RO(max_password_length);
142 
143 static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
144 {
145 	if (!strcmp(kobj->name, SETUP_PASSWD))
146 		return sysfs_emit(buf, "%s\n", BIOS_ADMIN);
147 
148 	if (!strcmp(kobj->name, POWER_ON_PASSWD))
149 		return sysfs_emit(buf, "%s\n", POWER_ON);
150 
151 	return -EIO;
152 }
153 
154 static struct kobj_attribute password_role = __ATTR_RO(role);
155 
156 static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribute *attr,
157 			      char *buf)
158 {
159 	int i = get_password_instance_id(kobj);
160 
161 	if (i < 0)
162 		return i;
163 
164 	if (bioscfg_drv.password_data[i].mechanism != PASSWORD)
165 		return -EINVAL;
166 
167 	return sysfs_emit(buf, "%s\n", PASSWD_MECHANISM_TYPES);
168 }
169 
170 static struct kobj_attribute password_mechanism = __ATTR_RO(mechanism);
171 
172 ATTRIBUTE_VALUES_PROPERTY_SHOW(encodings, password, SEMICOLON_SEP);
173 static struct kobj_attribute password_encodings_val = __ATTR_RO(encodings);
174 
175 static struct attribute *password_attrs[] = {
176 	&password_is_password_set.attr,
177 	&password_min_password_length.attr,
178 	&password_max_password_length.attr,
179 	&password_current_password.attr,
180 	&password_new_password.attr,
181 	&password_role.attr,
182 	&password_mechanism.attr,
183 	&password_encodings_val.attr,
184 	NULL
185 };
186 
187 static const struct attribute_group password_attr_group = {
188 	.attrs = password_attrs
189 };
190 
191 int hp_alloc_password_data(void)
192 {
193 	bioscfg_drv.password_instances_count = hp_get_instance_count(HP_WMI_BIOS_PASSWORD_GUID);
194 	bioscfg_drv.password_data = kzalloc_objs(*bioscfg_drv.password_data,
195 						 bioscfg_drv.password_instances_count);
196 	if (!bioscfg_drv.password_data) {
197 		bioscfg_drv.password_instances_count = 0;
198 		return -ENOMEM;
199 	}
200 
201 	return 0;
202 }
203 
204 /* Expected Values types associated with each element */
205 static const acpi_object_type expected_password_types[] = {
206 	[NAME] = ACPI_TYPE_STRING,
207 	[VALUE] = ACPI_TYPE_STRING,
208 	[PATH] = ACPI_TYPE_STRING,
209 	[IS_READONLY] = ACPI_TYPE_INTEGER,
210 	[DISPLAY_IN_UI] = ACPI_TYPE_INTEGER,
211 	[REQUIRES_PHYSICAL_PRESENCE] = ACPI_TYPE_INTEGER,
212 	[SEQUENCE] = ACPI_TYPE_INTEGER,
213 	[PREREQUISITES_SIZE] = ACPI_TYPE_INTEGER,
214 	[PREREQUISITES] = ACPI_TYPE_STRING,
215 	[SECURITY_LEVEL] = ACPI_TYPE_INTEGER,
216 	[PSWD_MIN_LENGTH] = ACPI_TYPE_INTEGER,
217 	[PSWD_MAX_LENGTH] = ACPI_TYPE_INTEGER,
218 	[PSWD_SIZE] = ACPI_TYPE_INTEGER,
219 	[PSWD_ENCODINGS] = ACPI_TYPE_STRING,
220 	[PSWD_IS_SET] = ACPI_TYPE_INTEGER,
221 };
222 
223 static int hp_populate_password_elements_from_package(union acpi_object *password_obj,
224 						      int password_obj_count,
225 						      int instance_id)
226 {
227 	char *str_value = NULL;
228 	int value_len;
229 	int ret;
230 	u32 size;
231 	u32 int_value = 0;
232 	int elem;
233 	int eloc;
234 	int pos_values;
235 	struct password_data *password_data = &bioscfg_drv.password_data[instance_id];
236 
237 	if (!password_obj)
238 		return -EINVAL;
239 
240 	for (elem = 1, eloc = 1; elem < password_obj_count; elem++, eloc++) {
241 		/* ONLY look at the first PASSWORD_ELEM_CNT elements */
242 		if (eloc == PSWD_ELEM_CNT)
243 			goto exit_package;
244 
245 		switch (password_obj[elem].type) {
246 		case ACPI_TYPE_STRING:
247 			if (PREREQUISITES != elem && PSWD_ENCODINGS != elem) {
248 				ret = hp_convert_hexstr_to_str(password_obj[elem].string.pointer,
249 							       password_obj[elem].string.length,
250 							       &str_value, &value_len);
251 				if (ret)
252 					continue;
253 			}
254 			break;
255 		case ACPI_TYPE_INTEGER:
256 			int_value = (u32)password_obj[elem].integer.value;
257 			break;
258 		default:
259 			pr_warn("Unsupported object type [%d]\n", password_obj[elem].type);
260 			continue;
261 		}
262 
263 		/* Check that both expected and read object type match */
264 		if (expected_password_types[eloc] != password_obj[elem].type) {
265 			pr_err("Error expected type %d for elem %d, but got type %d instead\n",
266 			       expected_password_types[eloc], elem, password_obj[elem].type);
267 			kfree(str_value);
268 			return -EIO;
269 		}
270 
271 		/* Assign appropriate element value to corresponding field*/
272 		switch (eloc) {
273 		case VALUE:
274 			break;
275 		case PATH:
276 		case IS_READONLY:
277 		case DISPLAY_IN_UI:
278 		case REQUIRES_PHYSICAL_PRESENCE:
279 		case SEQUENCE:
280 		case PREREQUISITES_SIZE:
281 		case PREREQUISITES:
282 		case SECURITY_LEVEL:
283 			ret = hp_get_common_data_from_package(password_obj, password_obj_count,
284 							      &elem, &eloc, int_value, &str_value,
285 							      &password_data->common);
286 			if (ret)
287 				return ret;
288 			break;
289 		case PSWD_MIN_LENGTH:
290 			password_data->min_password_length = int_value;
291 			break;
292 		case PSWD_MAX_LENGTH:
293 			password_data->max_password_length = int_value;
294 			break;
295 		case PSWD_SIZE:
296 
297 			if (int_value > MAX_ENCODINGS_SIZE) {
298 				pr_warn("Password Encoding size value exceeded the maximum number of elements supported or data may be malformed\n");
299 				int_value = MAX_ENCODINGS_SIZE;
300 			}
301 			password_data->encodings_size = int_value;
302 
303 			/* This step is needed to keep the expected
304 			 * element list pointing to the right obj[elem].type
305 			 * when the size is zero. PSWD_ENCODINGS
306 			 * object is omitted by BIOS when the size is
307 			 * zero.
308 			 */
309 			if (int_value == 0)
310 				eloc++;
311 			break;
312 		case PSWD_ENCODINGS:
313 			size = min_t(u32, password_data->encodings_size, MAX_ENCODINGS_SIZE);
314 			for (pos_values = 0; pos_values < size; pos_values++) {
315 				if (elem + pos_values >= password_obj_count) {
316 					pr_err("Error elem-objects package is too small\n");
317 					return -EINVAL;
318 				}
319 
320 				ret = hp_convert_hexstr_to_str(password_obj[elem + pos_values].string.pointer,
321 							       password_obj[elem + pos_values].string.length,
322 							       &str_value, &value_len);
323 				if (ret)
324 					break;
325 
326 				strscpy(password_data->encodings[pos_values], str_value);
327 				kfree(str_value);
328 				str_value = NULL;
329 
330 			}
331 			if (size)
332 				elem += size - 1;
333 			break;
334 		case PSWD_IS_SET:
335 			password_data->is_enabled = int_value;
336 			break;
337 		default:
338 			pr_warn("Invalid element: %d found in Password attribute or data may be malformed\n", elem);
339 			break;
340 		}
341 
342 		kfree(str_value);
343 		str_value = NULL;
344 	}
345 
346 exit_package:
347 	kfree(str_value);
348 	return 0;
349 }
350 
351 /**
352  * hp_populate_password_package_data()
353  *	Populate all properties for an instance under password attribute
354  *
355  * @password_obj: ACPI object with password data
356  * @password_obj_count: Number of elements in @password_obj
357  * @instance_id: The instance to enumerate
358  * @attr_name_kobj: The parent kernel object
359  */
360 int hp_populate_password_package_data(union acpi_object *password_obj, int password_obj_count,
361 				      int instance_id,
362 				      struct kobject *attr_name_kobj)
363 {
364 	struct password_data *password_data = &bioscfg_drv.password_data[instance_id];
365 
366 	password_data->attr_name_kobj = attr_name_kobj;
367 
368 	hp_populate_password_elements_from_package(password_obj,
369 						   password_obj_count,
370 						   instance_id);
371 
372 	hp_friendly_user_name_update(password_data->common.path,
373 				     attr_name_kobj->name,
374 				     password_data->common.display_name,
375 				     sizeof(password_data->common.display_name));
376 
377 	if (!strcmp(attr_name_kobj->name, SETUP_PASSWD))
378 		return sysfs_create_group(attr_name_kobj, &password_attr_group);
379 
380 	return sysfs_create_group(attr_name_kobj, &password_attr_group);
381 }
382 
383 static int hp_populate_password_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
384 						     int instance_id)
385 {
386 	int values;
387 	int isreadonly;
388 	struct password_data *password_data = &bioscfg_drv.password_data[instance_id];
389 	int ret = 0;
390 
391 	/*
392 	 * Only data relevant to this driver and its functionality is
393 	 * read. BIOS defines the order in which each * element is
394 	 * read. Element 0 data is not relevant to this
395 	 * driver hence it is ignored. For clarity, all element names
396 	 * (DISPLAY_IN_UI) which defines the order in which is read
397 	 * and the name matches the variable where the data is stored.
398 	 *
399 	 * In earlier implementation, reported errors were ignored
400 	 * causing the data to remain uninitialized. It is not
401 	 * possible to determine if data read from BIOS is valid or
402 	 * not. It is for this reason functions may return a error
403 	 * without validating the data itself.
404 	 */
405 
406 	// VALUE:
407 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, password_data->current_password,
408 					sizeof(password_data->current_password));
409 	if (ret < 0)
410 		goto buffer_exit;
411 
412 	// COMMON:
413 	ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size,
414 					     &password_data->common);
415 	if (ret < 0)
416 		goto buffer_exit;
417 
418 	// PSWD_MIN_LENGTH:
419 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
420 					 &password_data->min_password_length);
421 	if (ret < 0)
422 		goto buffer_exit;
423 
424 	// PSWD_MAX_LENGTH:
425 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
426 					 &password_data->max_password_length);
427 	if (ret < 0)
428 		goto buffer_exit;
429 
430 	// PSWD_SIZE:
431 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
432 					 &password_data->encodings_size);
433 	if (ret < 0)
434 		goto buffer_exit;
435 
436 	if (password_data->encodings_size > MAX_ENCODINGS_SIZE) {
437 		/* Report a message and limit possible values size to maximum value */
438 		pr_warn("Password Encoding size value exceeded the maximum number of elements supported or data may be malformed\n");
439 		password_data->encodings_size = MAX_ENCODINGS_SIZE;
440 	}
441 
442 	// PSWD_ENCODINGS:
443 	for (values = 0; values < password_data->encodings_size; values++) {
444 		ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size,
445 						password_data->encodings[values],
446 						sizeof(password_data->encodings[values]));
447 		if (ret < 0)
448 			break;
449 	}
450 
451 	// PSWD_IS_SET:
452 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size, &isreadonly);
453 	if (ret < 0)
454 		goto buffer_exit;
455 
456 	password_data->is_enabled = isreadonly ? true : false;
457 
458 buffer_exit:
459 	return ret;
460 }
461 
462 /**
463  * hp_populate_password_buffer_data()
464  * Populate all properties for an instance under password object attribute
465  *
466  * @buffer_ptr: Buffer pointer
467  * @buffer_size: Buffer size
468  * @instance_id: The instance to enumerate
469  * @attr_name_kobj: The parent kernel object
470  */
471 int hp_populate_password_buffer_data(u8 *buffer_ptr, u32 *buffer_size, int instance_id,
472 				     struct kobject *attr_name_kobj)
473 {
474 	struct password_data *password_data = &bioscfg_drv.password_data[instance_id];
475 	int ret = 0;
476 
477 	password_data->attr_name_kobj = attr_name_kobj;
478 
479 	/* Populate Password attributes */
480 	ret = hp_populate_password_elements_from_buffer(buffer_ptr, buffer_size,
481 							instance_id);
482 	if (ret < 0)
483 		return ret;
484 
485 	hp_friendly_user_name_update(password_data->common.path,
486 				     attr_name_kobj->name,
487 				     password_data->common.display_name,
488 				     sizeof(password_data->common.display_name));
489 	if (!strcmp(attr_name_kobj->name, SETUP_PASSWD))
490 		return sysfs_create_group(attr_name_kobj, &password_attr_group);
491 
492 	return sysfs_create_group(attr_name_kobj, &password_attr_group);
493 }
494 
495 /**
496  * hp_exit_password_attributes() - Clear all attribute data
497  *
498  * Clears all data allocated for this group of attributes
499  */
500 void hp_exit_password_attributes(void)
501 {
502 	int instance_id;
503 
504 	for (instance_id = 0; instance_id < bioscfg_drv.password_instances_count;
505 	     instance_id++) {
506 		struct kobject *attr_name_kobj =
507 			bioscfg_drv.password_data[instance_id].attr_name_kobj;
508 
509 		if (attr_name_kobj)
510 			sysfs_remove_group(attr_name_kobj,
511 					   &password_attr_group);
512 	}
513 	bioscfg_drv.password_instances_count = 0;
514 	kfree(bioscfg_drv.password_data);
515 	bioscfg_drv.password_data = NULL;
516 }
517