【问题标题】:Is this usage of mlockall correct?mlockall 的这种用法正确吗?
【发布时间】:2012-10-20 16:04:54
【问题描述】:

下面的程序对 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 <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);
}

【问题讨论】:

  • 请缩进你的程序。它会更容易阅读。同时使用 bufbuffer 标识符会产生误导。
  • 我阅读您的问题和 cmets 的次数越多,我就越发现您很困惑,真的应该花几个小时来阅读材料。正如我 [mis] 理解的那样,您当前的代码非常不安全......(从“秘密数据”的角度来看)。

标签: c memory encryption trace


【解决方案1】:

您对mlockall 的使用可能是正确的。既然你给了MCL_FUTURE,任何间接的malloc(例如fopen)也会被关注-但是这些malloc-s可能需要mmap(并且这些mmap系统调用可能会失败,例如因为缺乏RAM)。

但是你为什么不让你的buf 成为一个本地的int 变量呢?

而且我不明白为什么使用mlockall 会“避免在硬盘上留下任何密钥文件的痕迹”;密钥文件肯定在文件系统中(并且可能在某些内核文件缓存中),这会在某些磁盘上留下痕迹(除非您使用例如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 在做什么,他们正在添加另一个缓冲区,这将保存你的秘密数据,甚至可能在fclose 之后。

此外,您可能想要定义(至少在您的头脑中和明确的 cmets 中)您的 trusted computing base 是什么。

另外,cryptography 是一门非常困难的科学。请使用现有的密码库,而不是发明自己的加密(这真的是儿戏)。如果您想成为密码学家,请获得该领域的博士学位。 (我确实建议您在一次性键盘上使用一些加密库,而不仅仅是 xor)。

【讨论】:

  • 我的理解是mlockall 意味着有问题的文件只能在 RAM 中打开,因此一旦断电/计算机关闭就会被删除。缩进问题坦率地说是复制粘贴代码到此处并添加 4 个空格以使其显示为代码的结果,但我对给您带来的不便表示歉意。我没有创建任何我承认远远超出我理解的算法,但是一次性垫程序相对简单,例如我的。问题在于密钥生成。但感谢您的意见,如果我对 mlockall 的理解有问题,请纠正我。
  • 你的理解是非常错误的。请花几个小时阅读材料。我给了几个链接。
猜你喜欢
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2016-07-30
相关资源
最近更新 更多