summaryrefstree
path: root/kernel/security/defex_lsm/defex_safeplace.c
blob: 10e0189d35700976de9491128e9849f3e409926c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
*/

#include <linux/crc32.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include "include/defex_internal.h"

struct defex_safeplace *global_safeplace_obj;

#ifdef DEFEX_SYSFS_ENABLE
static struct kobj_type safeplace_ktype;

ssize_t task_defex_safeplace_attr_show(struct kobject *kobj,
		struct attribute *attr, char *buf)
{
	struct safeplace_attribute *attribute;
	struct defex_safeplace *safeplace;

	safeplace = to_safeplace_obj(kobj);
	attribute = to_safeplace_attr(attr);

	if (!attribute->show)
		return -EIO;

	return attribute->show(safeplace, attribute, buf);
}

ssize_t safeplace_status_show(struct defex_safeplace *safeplace_obj,
			      struct safeplace_attribute *attr, char *buf)
{
	return snprintf(buf, MAX_LEN, "%u\n", safeplace_obj->status);
}


ssize_t task_defex_safeplace_attr_store(struct kobject *kobj,
		struct attribute *attr, const char *buf, size_t len)
{
	struct safeplace_attribute *attribute;
	struct defex_safeplace *safeplace;

	safeplace = to_safeplace_obj(kobj);
	attribute = to_safeplace_attr(attr);

	if (!attribute->store)
		return -EIO;

	return attribute->store(safeplace, attribute, buf, len);
}

static void task_defex_safeplace_release(struct kobject *kobj)
{
	struct defex_safeplace *safeplace_obj;

	safeplace_obj = to_safeplace_obj(kobj);
	kfree(safeplace_obj);
}
#endif /* DEFEX_SYSFS_ENABLE */

ssize_t safeplace_status_store(struct defex_safeplace *safeplace_obj,
		struct safeplace_attribute *attr, const char *buf, size_t count)
{
	int ret;
	unsigned int status;

	ret = kstrtouint(buf, 10, &status);
	if (ret != 0 || status > 2)
		return -EINVAL;

	safeplace_obj->status = status;
	return count;
}

struct defex_safeplace *task_defex_create_safeplace_obj(struct kset *defex_kset)
{
	struct defex_safeplace *safeplace;

	safeplace = kzalloc(sizeof(*safeplace), GFP_KERNEL);
	if (!safeplace)
		return NULL;

	safeplace->kobj.kset = defex_kset;
#ifdef DEFEX_SYSFS_ENABLE
	if (kobject_init_and_add(&safeplace->kobj, &safeplace_ktype, NULL, "%s", "safeplace")) {
		kobject_put(&safeplace->kobj);
		return NULL;
	}

	kobject_uevent(&safeplace->kobj, KOBJ_ADD);
#endif /* DEFEX_SYSFS_ENABLE */
	safeplace->status = 0;
	return safeplace;
}

void task_defex_destroy_safeplace_obj(struct defex_safeplace *safeplace)
{
#ifdef DEFEX_SYSFS_ENABLE
	kobject_put(&safeplace->kobj);
#endif /* DEFEX_SYSFS_ENABLE */
}

#ifdef DEFEX_SYSFS_ENABLE
static struct safeplace_attribute safeplace_status_attribute =
	__ATTR(status, 0600, safeplace_status_show, safeplace_status_store);


static struct attribute *safeplace_default_attrs[] = {
	&safeplace_status_attribute.attr,
	NULL,
};

static const struct sysfs_ops safeplace_sysfs_ops = {
	.show = task_defex_safeplace_attr_show,
	.store = task_defex_safeplace_attr_store,
};

static struct kobj_type safeplace_ktype = {
	.sysfs_ops = &safeplace_sysfs_ops,
	.release = task_defex_safeplace_release,
	.default_attrs = safeplace_default_attrs,
};
#endif /* DEFEX_SYSFS_ENABLE */