1
0

vfs_crypto.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 The ChromiumOS Authors
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef _GNU_SOURCE
  5. #define _GNU_SOURCE
  6. #endif
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <linux/fs.h>
  10. #include <stdio.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <sys/ioctl.h>
  14. #include <unistd.h>
  15. extern char* program_invocation_short_name;
  16. int main(int argc, char** argv) {
  17. if (argc != 2) {
  18. printf("Usage: %s <path_to_directory>\n", program_invocation_short_name);
  19. return 1;
  20. }
  21. int dir = open(argv[1], O_DIRECTORY | O_CLOEXEC);
  22. if (dir < 0) {
  23. perror("Failed to open directory");
  24. return 1;
  25. }
  26. struct fscrypt_policy policy;
  27. int ret = ioctl(dir, FS_IOC_GET_ENCRYPTION_POLICY, &policy);
  28. if (ret < 0) {
  29. perror("FS_IOC_GET_ENCRYPTION_POLICY failed");
  30. return 1;
  31. }
  32. printf("File system encryption policy:\n");
  33. printf("\tversion = %#x\n", policy.version);
  34. printf("\tcontents_encryption_mode = %#x\n", policy.contents_encryption_mode);
  35. printf("\tfilenames_encryption_mode = %#x\n",
  36. policy.filenames_encryption_mode);
  37. printf("\tflags = %#x\n", policy.flags);
  38. printf("\tmaster_key_descriptor = 0x");
  39. for (int i = 0; i < FS_KEY_DESCRIPTOR_SIZE; ++i) {
  40. printf("%x", policy.master_key_descriptor[i]);
  41. }
  42. printf("\n");
  43. ret = ioctl(dir, FS_IOC_SET_ENCRYPTION_POLICY, &policy);
  44. if (ret < 0) {
  45. perror("FS_IOC_SET_ENCRYPTION_POLICY failed");
  46. return 1;
  47. }
  48. return 0;
  49. }