- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的程序使用一次一密加密对 2 个文件进行异或来创建输出文件。我尝试使用 mlockall
以避免从外部存储器源获取 key 文件时在硬盘上留下任何 key 文件痕迹。
来自 mlockall 手册页:
mlock() 和 mlockall() 分别锁定调用进程的部分或全部
虚拟地址空间进入 RAM,防止内存被分页到
交换区域。
我如何检查它是否正常工作以及我是否正确使用了 mlockall
?
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/mman.h>
int main(int argc, char **argv)
{
struct stat statbuf;
struct stat keybuf;
char buffer [20];
int key;
int data;
int output;
int count;
char ans;
int * buf;
FILE * keyfile;
FILE * sourcefile;
FILE * destfile;
if(geteuid() !=0)
{
printf("Root access is required to run this program\n\n");
exit(0);
}
if(argc<4)
{
printf("OTP-Bunny 1.0\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (0);
}
/* Check number of arguments. */
if(argc>4)
{
printf("Too many arguments.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
exit(1);
}
/* Allocate memory required by processes */
buf = (int*) malloc (sizeof(int));
if (buf == NULL)
{
perror("Error");
exit(1);
}
/* Lock down pages mapped to processes */
printf("Locking down processes\n");
if(mlockall (MCL_CURRENT | MCL_FUTURE) < 0)
{
perror("mlockall");
exit (1);
}
/* Check if sourcefile can be opened. */
if((sourcefile = fopen(argv[1], "rb"))== NULL)
{
printf("Can't open source file\n");
perror("Error");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
exit (1);
}
/* Get size of sourcefile */
fstat(fileno(sourcefile), &statbuf);
/* Check if keyfile can be opened. */
if((keyfile = fopen(argv[3], "rb"))== NULL)
{
printf("Can't open keyfile.\n");
perror("Error");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
exit(1);
}
/* Get size of keyfile */
fstat(fileno(keyfile), &keybuf);
/* Check if keyfile is the same size as, or bigger than the sourcefile */
if((keybuf.st_size) < (statbuf.st_size))
{
printf("Source file is larger than keyfile.\n");
printf("This significantly reduces cryptographic strength.\n");
printf("Do you wish to continue? (Y/N)\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%c", &ans);
if(ans == 'n' || ans == 'N')
{
exit (1);
}
if(ans == 'y' || ans == 'Y')
{
printf("Proceeding with Encryption/Decryption.\n");
}
else
{
printf("No option selected. Exiting...\n");
exit (1);
}
}
/* Check if destfile can be opened. */
if((destfile = fopen(argv[2], "wb"))== NULL)
{
printf("Can't open output file.\n");
perror("Error");
exit(1);
}
/* Encrypt/Decrypt and write to output file. */
while(count < (statbuf.st_size))
{
key=fgetc(keyfile);
data=fgetc(sourcefile);
output=(key^data);
fputc(output,destfile);
count++;
}
/* Close files. */
fclose(keyfile);
fclose(sourcefile);
fclose(destfile);
printf("Encryption/Decryption Complete.\n\n");
/* delete keyfile option. */
printf("Do you wish to delete the keyfile? (Y/N)\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%c", &ans);
if(ans == 'y' || ans == 'Y')
{
if ( remove(argv[3]) == 0)
{
printf("File deleted successfully.\n");
}
else
{
printf("Unable to delete the file.\n");
perror("Error");
exit(1);
}
}
/* cleanup */
printf("Releasing memory\n");
free (buf);
return(0);
}
最佳答案
您对 mlockall
的使用可能是正确的。由于您提供了MCL_FUTURE
,任何间接malloc
(例如通过fopen
)也会受到关注 - 但这些malloc
-s可能需要 mmap
(并且这些 mmap
系统调用可能会失败,例如由于缺少 RAM)。
但是为什么不将 buf
设为本地 int
变量呢?
而且我不明白为什么使用mlockall
会“避免在硬盘驱动器上留下 key 文件的任何痕迹”; key 文件肯定位于文件系统中(可能在某些内核文件缓存中),它会在某些磁盘上留下痕迹(除非您使用例如 tmpfs 文件系统)。 mlopckall(2)处理 process ' ( virtual memory ) address space ,但文件与 file systems 相关,在 Linux 上通常有 kernel缓冲区和cache .
由于缺乏缩进,我往往会发现你的程序难以阅读,而且我不明白它到底是做什么的以及 mlockall
的相关性是什么。最好编辑您的问题来解释您的程序的预期目的。
你真的应该读一本好书,比如Advanced Linux Programming和 Advanced Unix Programming 。看来你缺少一些基本概念;我不明白您为什么使用 mlockall
。
也许您可以使用较低级别的系统调用,例如 mmap(2)访问您的敏感数据(并尽快munmap(2)
它,也许之前清除它)。您不知道 fopen
或 fgetc
正在做什么,它们正在添加另一个缓冲区,这将保留您的 secret 数据,甚至可能在 fclose
之后>.
此外,您可能想要定义(至少在您的脑海中和明确的注释中)您的 trusted computing base 是什么? .
此外,cryptography是一门非常困难的科学。请使用现有的加密库,而不是发明自己的加密(这实际上是一个 child 子的游戏)。如果您想成为一名密码学家,请获得该领域的博士学位。(我确实建议您在一次一垫本中使用一些加密库,而不仅仅是异或)。
关于c - mlockall 的这种用法正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990214/
下面的程序使用一次一密加密对 2 个文件进行异或来创建输出文件。我尝试使用 mlockall 以避免从外部存储器源获取 key 文件时在硬盘上留下任何 key 文件痕迹。 来自 mlockall 手册
我对 mlockall 手册中出现的以下句子感到有点困惑。 mlockall() locks all pages mapped into the address space of the callin
我会在实时进程上执行mlockall to lock the memory pages没有root权限。由于用于最大锁定内存的用户 ulimit 确实很低(出于安全考虑,这没问题),我将通过 root
我收到错误:mlockall 失败:无法分配内存调用函数 mlockall() 时: if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
问题 在尝试减少/消除应用程序中轻微页面错误的过程中,我发现了一个令人困惑的现象;也就是说,即使我认为我已经采取了足够的措施来防止页面错误,但我反复触发写入同一地址的小页面错误。 背景 根据建议her
mlockall 在我的内核 3.0 上的手册页说 mlockall() locks all pages mapped into the address space of the calling pr
我正在阅读 mlockall() 的联机帮助页:http://man7.org/linux/man-pages/man2/mlock.2.html 它提到 Real-time processes th
我有一个问题,mlockall 直接返回非特权用户(root 以外的每个用户)-1 我用来运行它的应用程序来自 Stackoverflow 站点:What happens when you run o
我正在开发一个需要大量内存才能进行批处理的 C++ 应用程序。(> 20GB) 我的一些客户遇到了内存限制,有时操作系统开始交换并且总运行时间加倍或更糟。 我读到我可以使用 mlockall 来防止进
我看到 ElasticSearch 提供了 mlockall 选项,它允许将 JVM 堆保留在物理内存中而不是虚拟内存(可能是也可能不是物理内存),并避免交换堆。 Solr 有这样的选项吗? 最佳答案
我是一名优秀的程序员,十分优秀!