Debian Codegraph @

core/jni/com_android_internal_security_VerityUtils.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "VerityUtils"
18
19 #include <android-base/unique_fd.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <linux/fs.h>
23 #include <linux/fsverity.h>
24 #include <linux/stat.h>
25 #include <nativehelper/JNIHelp.h>
26 #include <nativehelper/ScopedPrimitiveArray.h>
27 #include <nativehelper/ScopedUtfChars.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <utils/Log.h>
33
34 #include <type_traits>
35
36 #include "jni.h"
37
38 namespace android {
39
40 namespace {
41
42 int enableFsverity(JNIEnv *env, jobject /* clazz */, jstring filePath, jbyteArray signature) {
43 ScopedUtfChars path(env, filePath);
44 if (path.c_str() == nullptr) {
45 return EINVAL;
46 }
47 ::android::base::unique_fd rfd(open(path.c_str(), O_RDONLY | O_CLOEXEC));
48 if (rfd.get() < 0) {
49 return errno;
50 }
51
52 fsverity_enable_arg arg = {};
53 arg.version = 1;
54 arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256; // hardcoded in measureFsverity below
55 arg.block_size = 4096;
56 arg.salt_size = 0;
57 arg.salt_ptr = reinterpret_cast<uintptr_t>(nullptr);
58
59 if (signature != nullptr) {
60 ScopedByteArrayRO signature_bytes(env, signature);
61 if (signature_bytes.get() == nullptr) {
62 return EINVAL;
63 }
64 arg.sig_size = signature_bytes.size();
65 arg.sig_ptr = reinterpret_cast<uintptr_t>(signature_bytes.get());
66 } else {
67 arg.sig_size = 0;
68 arg.sig_ptr = reinterpret_cast<uintptr_t>(nullptr);
69 }
70
71 if (ioctl(rfd.get(), FS_IOC_ENABLE_VERITY, &arg) < 0) {
72 return errno;
73 }
74 return 0;
75 }
76
77 // Returns whether the file has fs-verity enabled.
78 // 0 if it is not present, 1 if is present, and -errno if there was an error.
79 int statxForFsverity(JNIEnv *env, jobject /* clazz */, jstring filePath) {
80 ScopedUtfChars path(env, filePath);
81
82 // Call statx and check STATX_ATTR_VERITY.
83 struct statx out = {};
84 if (statx(AT_FDCWD, path.c_str(), 0 /* flags */, STATX_ALL, &out) != 0) {
85 return -errno;
86 }
87
88 if (out.stx_attributes_mask & STATX_ATTR_VERITY) {
89 return (out.stx_attributes & STATX_ATTR_VERITY) != 0;
90 }
91
92 // STATX_ATTR_VERITY is not supported for the file path.
93 // In this case, call ioctl(FS_IOC_GETFLAGS) and check FS_VERITY_FL.
94 ::android::base::unique_fd rfd(open(path.c_str(), O_RDONLY | O_CLOEXEC));
95 if (rfd.get() < 0) {
96 ALOGE("open failed at %s", path.c_str());
97 return -errno;
98 }
99
100 unsigned int flags;
101 if (ioctl(rfd.get(), FS_IOC_GETFLAGS, &flags) < 0) {
102 ALOGE("ioctl(FS_IOC_GETFLAGS) failed at %s", path.c_str());
103 return -errno;
104 }
105
106 return (flags & FS_VERITY_FL) != 0;
107 }
108
109 int measureFsverity(JNIEnv *env, jobject /* clazz */, jstring filePath, jbyteArray digest) {
110 static constexpr auto kDigestSha256 = 32;
111 using Storage = std::aligned_storage_t<sizeof(fsverity_digest) + kDigestSha256>;
112
113 Storage bytes;
114 fsverity_digest *data = reinterpret_cast<fsverity_digest *>(&bytes);
115 data->digest_size = kDigestSha256; // the only input/output parameter
116
117 ScopedUtfChars path(env, filePath);
118 ::android::base::unique_fd rfd(open(path.c_str(), O_RDONLY | O_CLOEXEC));
119 if (rfd.get() < 0) {
120 return rfd.get();
121 }
122 if (auto err = ioctl(rfd.get(), FS_IOC_MEASURE_VERITY, data); err < 0) {
123 return err;
124 }
125
126 if (data->digest_algorithm != FS_VERITY_HASH_ALG_SHA256) {
127 return -EINVAL;
128 }
129
130 if (digest != nullptr && data->digest_size > 0) {
131 auto digestSize = env->GetArrayLength(digest);
132 if (data->digest_size > digestSize) {
133 return -E2BIG;
134 }
135 env->SetByteArrayRegion(digest, 0, data->digest_size, (const jbyte *)data->digest);
136 }
137
138 return 0;
139 }
140 const JNINativeMethod sMethods[] = {
141 {"enableFsverityNative", "(Ljava/lang/String;[B)I", (void *)enableFsverity},
142 {"statxForFsverityNative", "(Ljava/lang/String;)I", (void *)statxForFsverity},
143 {"measureFsverityNative", "(Ljava/lang/String;[B)I", (void *)measureFsverity},
144 };
145
146 } // namespace
147
148 int register_com_android_internal_security_VerityUtils(JNIEnv *env) {
149 return jniRegisterNativeMethods(env, "com/android/internal/security/VerityUtils", sMethods,
150 NELEM(sMethods));
151 }
152
153 } // namespace android