1 /*
2  * Copyright (C) 2021 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 "ArtifactsSignedTest"
18 
19 #include "jni.h"
20 #include <nativehelper/JNIHelp.h>
21 #include <nativehelper/ScopedUtfChars.h>
22 
23 #include <android/log.h>
24 
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 
31 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
32 
33 extern "C" JNIEXPORT jboolean JNICALL
Java_com_android_tests_odsign_ArtifactsSignedTest_hasFsverityNative(JNIEnv * env,jobject,jstring filePath)34 Java_com_android_tests_odsign_ArtifactsSignedTest_hasFsverityNative(
35     JNIEnv *env, jobject /*thiz*/, jstring filePath) {
36   ScopedUtfChars path(env, filePath);
37 
38   struct statx out = {};
39   if (statx(AT_FDCWD, path.c_str(), /*flags=*/ 0, STATX_ALL, &out) != 0) {
40     ALOGE("statx failed at %s", path.c_str());
41     return JNI_FALSE;
42   }
43 
44   // Make sure we actually support ATTR_VERITY
45   if ((out.stx_attributes_mask & STATX_ATTR_VERITY) == 0) {
46     ALOGE("STATX_ATTR_VERITY not supported by kernel");
47     return JNI_FALSE;
48   }
49 
50   return (out.stx_attributes & STATX_ATTR_VERITY) != 0 ? JNI_TRUE : JNI_FALSE;
51 }
52