0

以下のプログラムは、2つのファイルをXORして、ワンタイムパッド暗号化を使用して出力ファイルを作成します。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);
}
4

1 に答える 1

1

の使い方mlockallはおそらく正しいでしょう。MCL_FUTURE間接的なものmalloc(たとえば by fopen) も考慮されるため、これらのmalloc-s が必要になる場合がありますmmap(これらのmmapシステムコールは、たとえば RAM の不足により失敗する可能性があります)。

bufしかし、ローカルint変数を作成してみませんか?

mlockallそして、使用すると「キーファイルの痕跡がハードドライブに残されるのを避ける」理由がわかりません。キーファイルは確かにファイルシステム (およびおそらくカーネルファイルキャッシュ) にあり、一部のディスクにトレースを残します (たとえば、tmpfsファイルシステムを使用しない限り)。mlopckall(2)プロセスの (仮想メモリ)アドレス空間を扱いますが、ファイルはファイル システムに関連しており、Linux では通常、カーネルバッファとキャッシュがあります。

インデントがないため、あなたのプログラムは読みにくいと思う傾向があり、それが何をしているのか、mlockall. 質問を編集して、プログラムの意図した目的を説明するとよいでしょう。

Advanced Linux ProgrammingAdvanced Unix Programmingなどの優れた本を読むべきです。いくつかの基本的な概念が欠けているようです。を使用している理由がわかりませんmlockall

おそらく、機密データにアクセスするためにmmap(2)のような低レベルのシステムコールを使用するかもしれません(そしてmunmap(2)、できるだけ早く、おそらく前にクリアします)。あなたは正確に何をしているのか、何をしているのかわかりませんfopen.fgetc彼らは別のバッファを追加していますfclose.

また、信頼できるコンピューティング ベースとは何かを (少なくとも頭の中で明示的なコメントで) 定義することもできます。

また、暗号は非常に難しい科学です。独自の暗号化を発明する代わりに、既存の暗号化ライブラリを使用してください (これは本当に子供の遊びです)。暗号学者になりたい場合は、そのドメインで博士号を取得してください。(xorだけでなく、ワンタイムパッドで暗号化ライブラリを使用することをお勧めします)。

于 2012-10-20T16:55:48.437 に答える