gpt4 book ai didi

c - mlockall 的这种用法正确吗?

转载 作者:行者123 更新时间:2023-11-30 15:54:25 25 4
gpt4 key购买 nike

下面的程序使用一次一密加密对 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 ProgrammingAdvanced Unix Programming 。看来你缺少一些基本概念;我不明白您为什么使用 mlockall

也许您可以使用较低级别的系统调用,例如 mmap(2)访问您的敏感数据(并尽快munmap(2)它,也许之前清除它)。您不知道 fopenfgetc 正在做什么,它们正在添加另一个缓冲区,这将保留您的 secret 数据,甚至可能在 fclose 之后>.

此外,您可能想要定义(至少在您的脑海中和明确的注释中)您的 trusted computing base 是什么? .

此外,cryptography是一门非常困难的科学。请使用现有的加密库,而不是发明自己的加密(这实际上是一个 child 子的游戏)。如果您想成为一名密码学家,请获得该领域的博士学位。(我确实建议您在一次一垫本中使用一些加密库,而不仅仅是异或)。

关于c - mlockall 的这种用法正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com