summaryrefstree
path: root/kernel/security/proca/proca_identity.c
blob: 2c71a2e8fc2b9be4916fc556a244740103a1a834 (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
/*
 * PROCA identity
 *
 * Copyright (C) 2018 Samsung Electronics, Inc.
 * Ivan Vorobiov, <i.vorobiov@samsung.com>
 * Egor Uleyskiy, <e.uleyskiy@samsung.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "proca_identity.h"
#include "proca_certificate.h"
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/string.h>

int init_proca_identity(struct proca_identity *identity, struct file *file,
			char *xattr_value, const size_t xattr_size,
			struct proca_certificate *parsed_cert)
{
	int rc = 0;

	if (!file)
		return -EINVAL;

	get_file(file);
	identity->file = file;
	identity->certificate = xattr_value;
	identity->certificate_size = xattr_size;

	if (parsed_cert)
		identity->parsed_cert = *parsed_cert;
	else
		memset(&identity->parsed_cert,
				0, sizeof(identity->parsed_cert));

	return rc;
}

int proca_identity_copy(struct proca_identity *dst, struct proca_identity *src)
{
	int rc = 0;

	BUG_ON(!dst || !src);

	memset(dst, 0, sizeof(*dst));

	get_file(src->file);
	dst->file = src->file;

	if (src->certificate) {
		dst->certificate = kmemdup(
				src->certificate,
				src->certificate_size,
				GFP_KERNEL);
		dst->certificate_size =
				src->certificate_size;

		if (unlikely(!dst->certificate)) {
			fput(src->file);
			return -ENOMEM;
		}
	}

	rc = proca_certificate_copy(&dst->parsed_cert, &src->parsed_cert);
	if (rc != 0) {
		kfree(dst->certificate);
		fput(src->file);
	}

	return rc;
}

void deinit_proca_identity(struct proca_identity *identity)
{
	if (unlikely(!identity))
		return;

	deinit_proca_certificate(&identity->parsed_cert);
	if (identity->file)
		fput(identity->file);
	kfree(identity->certificate);
}