这种mlockall的用法是否正确?

下面的程序XORs 2文件使用一次填充加密创建输出文件。 我试图使用mlockall ,以避免从外部内存源获取密钥文件时硬盘上留下任何密钥文件的痕迹。

从mlockall手册页:

mlock() and mlockall() respectively lock part or all of the calling process's virtual address space into RAM, preventing that memory from being paged to the swap area.

我如何检查它是否正常工作并且我正确使用了mlockall

 #include  #include  #include  #include  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   \n"); return (0); } /* Check number of arguments. */ if(argc>4) { printf("Too many arguments.\n"); printf("USAGE: OTP   \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   \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   \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会“避免硬盘上留下任何密钥文件的痕迹”; 密钥文件肯定在文件系统中(可能在某些内核文件缓存中),这会在某些磁盘上留下痕迹(除非您使用例如tmpfs文件系统)。 mlopckall(2)处理进程 ‘( 虚拟内存 ) 地址空间 ,但文件与文件系统有关 ,在Linux上通常有内核缓冲区和缓存 。

由于缺少缩进,我倾向于发现你的程序难以阅读,我不明白它的作用是什么以及mlockall的相关性是mlockall 。 编辑您的问题以解释您的计划的预期目的会很好。

你真的应该读一本好书,比如高级Linux编程和高级Unix编程 。 你似乎缺少一些基本概念; 我不明白你为什么要使用mlockall

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

此外,您可能希望定义(至少在您的头脑和显式注释中)您的可信计算基础 。

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