parser_lib.c 6.1 KB
Newer Older
R
Roberto Sassu 已提交
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
/*
 * Copyright (C) 2019-2020 Huawei Technologies Duesseldorf GmbH
 *
 * Author: Roberto Sassu <roberto.sassu@huawei.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 * File: parser_lib.c
 *      Parser library.
 */

#include <errno.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <sys/capability.h>

#include "compact_list.h"
#include "parser_lib.h"
#include "crypto.h"
#include "xattr.h"
#include "evm.h"
#include "cap.h"

int add_digest(int fd, struct list_head *head, u16 type, u16 modifiers,
R
Roberto Sassu 已提交
28
	       u16 algo, u8 *digest)
R
Roberto Sassu 已提交
29
{
R
Roberto Sassu 已提交
30
	struct list_struct *list;
R
Roberto Sassu 已提交
31

R
Roberto Sassu 已提交
32 33 34 35
	if (!digest) {
		pr_err("TLV compact list, invalid data\n");
		return -ENOENT;
	}
R
Roberto Sassu 已提交
36

R
Roberto Sassu 已提交
37 38 39
	list = compact_list_init(head, type, modifiers, algo, false);
	if (!list)
		return -ENOMEM;
R
Roberto Sassu 已提交
40

R
Roberto Sassu 已提交
41
	return compact_list_add_digest(fd, list, digest);
R
Roberto Sassu 已提交
42 43 44
}

int calc_metadata_digest(int fd, struct list_head *head, u16 type,
R
Roberto Sassu 已提交
45 46 47
			 u16 modifiers, u16 algo, u8 *digest, u8 *evm_digest,
			 char *path, uid_t uid, gid_t gid, mode_t mode,
			 char *obj_label, char *caps)
R
Roberto Sassu 已提交
48
{
R
Roberto Sassu 已提交
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
	cap_t c;
	struct vfs_cap_data rawvfscap;
	enum hash_algo evm_algo = HASH_ALGO_SHA256;
	u8 ima_xattr[2 + SHA512_DIGEST_SIZE], *caps_bin = NULL;
	int ret, ima_xattr_len, rawvfscap_len = 0;

	if (!digest || !path) {
		pr_err("TLV compact list, missing data\n");
		return -ENOENT;
	}

	ret = gen_write_ima_xattr(ima_xattr, &ima_xattr_len, path, algo, digest,
				  (modifiers & (1 << COMPACT_MOD_IMMUTABLE)),
				  false);
	if (ret < 0)
		return ret;

	if (ima_canonical_fmt) {
		uid = le32_to_cpu(uid);
		gid = le32_to_cpu(gid);
		mode = le32_to_cpu(mode);
	}

	if (caps) {
		c = cap_from_text(caps);
		if (!c)
			return -EINVAL;

		ret = _fcaps_save(&rawvfscap, c, &rawvfscap_len);

		cap_free(c);

		if (ret < 0)
			return -EINVAL;
	}

	ret = evm_calc_hmac_or_hash(evm_algo, evm_digest,
				    obj_label ? strlen(obj_label) + 1 : 0,
				    obj_label, ima_xattr_len, ima_xattr,
				    rawvfscap_len, (u8 *)&rawvfscap,
				    uid, gid, mode);
	free(caps_bin);
	return ret;
R
Roberto Sassu 已提交
92 93 94 95
}

int add_metadata_digest(int fd, struct list_head *head, u16 modifiers, u8 *evm_digest)
{
R
Roberto Sassu 已提交
96 97
	enum hash_algo evm_algo = HASH_ALGO_SHA256;
	struct list_struct *list_metadata;
R
Roberto Sassu 已提交
98

R
Roberto Sassu 已提交
99 100 101 102
	list_metadata = compact_list_init(head, COMPACT_METADATA, modifiers, evm_algo,
					  false);
	if (!list_metadata)
		return -ENOMEM;
R
Roberto Sassu 已提交
103

R
Roberto Sassu 已提交
104
	return compact_list_add_digest(fd, list_metadata, evm_digest);
R
Roberto Sassu 已提交
105 106 107
}

int add_ima_xattr(int fd, struct list_head *head, u16 type, u16 modifiers,
R
Roberto Sassu 已提交
108
		  u16 algo, u8 *digest, char *path)
R
Roberto Sassu 已提交
109
{
R
Roberto Sassu 已提交
110 111
	u8 ima_xattr[2 + SHA512_DIGEST_SIZE];
	int ret, ima_xattr_len;
R
Roberto Sassu 已提交
112

R
Roberto Sassu 已提交
113 114 115 116
	if (!digest || !path) {
		pr_err("TLV compact list, missing data\n");
		return -ENOENT;
	}
R
Roberto Sassu 已提交
117

R
Roberto Sassu 已提交
118 119 120 121 122
	ret = gen_write_ima_xattr(ima_xattr, &ima_xattr_len, path, algo, digest,
				  (modifiers & (1 << COMPACT_MOD_IMMUTABLE)),
				  true);
	if (ret < 0)
		pr_err("Cannot set IMA xattr for %s\n", path);
R
Roberto Sassu 已提交
123

R
Roberto Sassu 已提交
124
	return 0;
R
Roberto Sassu 已提交
125 126 127
}

int check_repair_xattr(char *path, char *xattr_name, void *xattr_value,
R
Roberto Sassu 已提交
128 129
		       int xattr_value_len, int ima_algo, int modifiers,
		       int repair)
R
Roberto Sassu 已提交
130
{
R
Roberto Sassu 已提交
131 132 133 134 135 136 137 138 139 140
	cap_t c;
	struct vfs_cap_data rawvfscap;
	void *cur_xattr_value = NULL;
	int cur_xattr_value_len;
	u8 ima_xattr[2 + SHA512_DIGEST_SIZE];
	int ret, rawvfscap_len, ima_xattr_len;

	if (!strcmp(xattr_name, XATTR_NAME_IMA)) {
		ret = gen_write_ima_xattr(ima_xattr, &ima_xattr_len, path,
				  ima_algo, xattr_value,
R
Roberto Sassu 已提交
141 142
                                  (modifiers & (1 << COMPACT_MOD_IMMUTABLE)),
                                  false);
R
Roberto Sassu 已提交
143 144
		if (ret < 0)
			return ret;
R
Roberto Sassu 已提交
145

R
Roberto Sassu 已提交
146 147 148 149 150 151
		xattr_value = &ima_xattr;
		xattr_value_len = ima_xattr_len;
	} else if (!strcmp(xattr_name, XATTR_NAME_CAPS) && xattr_value_len) {
		c = cap_from_text(xattr_value);
		if (!c)
			return -EINVAL;
R
Roberto Sassu 已提交
152

R
Roberto Sassu 已提交
153
		ret = _fcaps_save(&rawvfscap, c, &rawvfscap_len);
R
Roberto Sassu 已提交
154

R
Roberto Sassu 已提交
155
		cap_free(c);
R
Roberto Sassu 已提交
156

R
Roberto Sassu 已提交
157 158
		if (ret < 0)
			return -EINVAL;
R
Roberto Sassu 已提交
159

R
Roberto Sassu 已提交
160 161
		xattr_value = &rawvfscap;
		xattr_value_len = rawvfscap_len;
R
Roberto Sassu 已提交
162 163
        }

R
Roberto Sassu 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	cur_xattr_value_len = lgetxattr(path, xattr_name, NULL, 0);
	if (((cur_xattr_value_len == -1 && errno == ENODATA) ||
	    cur_xattr_value_len == 0) && !xattr_value_len)
		return 0;

	if (cur_xattr_value_len == -1 && errno != ENODATA) {
		pr_err("Path %s: cannot read %s xattr\n", path, xattr_name);
		return cur_xattr_value_len;
	}

	if (!xattr_value_len) {
		pr_err("Path %s: %s xattr defined, it should be removed\n",
		       path, xattr_name);

		if (repair) {
			ret = lremovexattr(path, xattr_name);
			if (ret < 0) {
				printf("Path: %s, cannot remove %s xattr\n",
				       path, xattr_name);
			}
		}

		return 0;
	}

	if (cur_xattr_value_len > 0) {
		cur_xattr_value = malloc(cur_xattr_value_len);
		if (!cur_xattr_value) {
			pr_err("Out of memory\n");
			return -ENOMEM;
		}
	}

	cur_xattr_value_len = lgetxattr(path, xattr_name, cur_xattr_value,
					cur_xattr_value_len);
	if (xattr_value_len != cur_xattr_value_len ||
	    memcmp(xattr_value, cur_xattr_value, xattr_value_len)) {
		pr_err("Path %s: %s xattr value mismatch\n", path, xattr_name);

		if (repair) {
			ret = lsetxattr(path, xattr_name, xattr_value,
					xattr_value_len, 0);
			if (ret) {
				pr_err("Path %s: failed to set %s xattr\n",
				       path, xattr_name);
				goto out;
			}
		}
	}

	ret = 0;
R
Roberto Sassu 已提交
215
out:
R
Roberto Sassu 已提交
216 217
	free(cur_xattr_value);
	return ret;
R
Roberto Sassu 已提交
218 219 220
}

int check_repair_attr(char *path, uid_t uid, gid_t gid, mode_t mode,
R
Roberto Sassu 已提交
221
		      int repair)
R
Roberto Sassu 已提交
222
{
R
Roberto Sassu 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	struct stat st;
	int ret;

	if (stat(path, &st) == -1) {
		pr_err("Path %s: file does not exist\n", path);
		return -ENOENT;
	}

	if (uid != st.st_uid || gid != st.st_gid) {
		if (uid != st.st_uid)
			pr_err("Path %s: uid mismatch, expected: %d, "
			       "current: %d\n", path, uid, st.st_uid);

		if (gid != st.st_gid)
			pr_err("Path %s: gid mismatch, expected: %d, "
			       "current: %d\n", path, gid, st.st_gid);

		if (repair) {
			ret = chown(path, uid, gid);
			if (ret < 0) {
				pr_err("Path %s: cannot set uid/gid\n", path);
				return ret;
			}
		}
	}

	if (mode != st.st_mode) {
		pr_err("Path %s: mode mismatch, expected: %d, current: %d\n",
		       path, mode, st.st_mode);

		if (repair) {
			ret = chmod(path, mode);
			if (ret < 0) {
				pr_err("Path %s: cannot set mode\n", path);
				return ret;
			}
		}
	}

	return 0;
R
Roberto Sassu 已提交
263
}