integrity_checker.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2009 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /**
  16. * @defgroup integrity_checker integrity_checker
  17. * @{ @ingroup utils
  18. */
  19. #ifndef INTEGRITY_CHECKER_H_
  20. #define INTEGRITY_CHECKER_H_
  21. #include "utils.h"
  22. typedef struct integrity_checker_t integrity_checker_t;
  23. typedef struct integrity_checksum_t integrity_checksum_t;
  24. /**
  25. * Struct to hold a precalculated checksum, implemented in the checksum library.
  26. */
  27. struct integrity_checksum_t {
  28. /* name of the checksum */
  29. char *name;
  30. /* size in bytes of the file on disk */
  31. size_t file_len;
  32. /* checksum of the file on disk */
  33. uint32_t file;
  34. /* size in bytes of executable segment in memory */
  35. size_t segment_len;
  36. /* checksum of the executable segment in memory */
  37. uint32_t segment;
  38. };
  39. /**
  40. * Code integrity checker to detect non-malicious file manipulation.
  41. *
  42. * The integrity checker reads the checksums from a separate library
  43. * libchecksum.so to compare the checksums.
  44. */
  45. struct integrity_checker_t {
  46. /**
  47. * Check the integrity of a file on disk.
  48. *
  49. * @param name name to lookup checksum
  50. * @param file path to file
  51. * @return TRUE if integrity tested successfully
  52. */
  53. bool (*check_file)(integrity_checker_t *this, char *name, char *file);
  54. /**
  55. * Build the integrity checksum of a file on disk.
  56. *
  57. * @param file path to file
  58. * @param len return length in bytes of file
  59. * @return checksum, 0 on error
  60. */
  61. uint32_t (*build_file)(integrity_checker_t *this, char *file, size_t *len);
  62. /**
  63. * Check the integrity of the code segment in memory.
  64. *
  65. * @param name name to lookup checksum
  66. * @param sym a symbol in the segment to check
  67. * @return TRUE if integrity tested successfully
  68. */
  69. bool (*check_segment)(integrity_checker_t *this, char *name, void *sym);
  70. /**
  71. * Build the integrity checksum of a code segment in memory.
  72. *
  73. * @param sym a symbol in the segment to check
  74. * @param len return length in bytes of code segment in memory
  75. * @return checksum, 0 on error
  76. */
  77. uint32_t (*build_segment)(integrity_checker_t *this, void *sym, size_t *len);
  78. /**
  79. * Check both, on disk file integrity and loaded segment.
  80. *
  81. * @param name name to lookup checksum
  82. * @param sym a symbol to look up library and segment
  83. * @return TRUE if integrity tested successfully
  84. */
  85. bool (*check)(integrity_checker_t *this, char *name, void *sym);
  86. /**
  87. * Destroy a integrity_checker_t.
  88. */
  89. void (*destroy)(integrity_checker_t *this);
  90. };
  91. /**
  92. * Create a integrity_checker instance.
  93. *
  94. * @param checksum_library library containing checksums
  95. */
  96. integrity_checker_t *integrity_checker_create(char *checksum_library);
  97. #endif /** INTEGRITY_CHECKER_H_ @}*/