[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: MD5File and devices
Here is a proposed patch that seems to do what is needed with the minimum
of fuss. there's still the issue of directories and their returning a
constant value no matter what the path. By giving what appears to be a
valid MD5 hash IMO we're luring the user into a false sense of security.
Better to return EISDIR instead. The really important fix for the time
being though is the addition of the O_NONBLOCK flag to open(). I'm
surprised we didn't get bit by this sooner.
Also fixed a few "return 0;" where the return type is char* which as I'm
watching cvs log messages should be NULL's instead.
Index: mdXhl.c
===================================================================
RCS file: /cvs/src/lib/libc/md/mdXhl.c,v
retrieving revision 1.9
diff -u -p -r1.9 mdXhl.c
--- mdXhl.c 1999/08/17 09:13:12 1.9
+++ mdXhl.c 2000/01/17 19:14:52
@@ -16,6 +16,7 @@ static char rcsid[] = "$OpenBSD: mdXhl.c
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <mdX.h>
@@ -35,7 +36,7 @@ MDXEnd(ctx, buf)
p = malloc(33);
if (!p)
return 0;
- MDXFinal(digest,ctx);
+ MDXFinal(digest, ctx);
for (i=0;i<16;i++) {
p[i+i] = hex[digest[i] >> 4];
p[i+i+1] = hex[digest[i] & 0x0f];
@@ -51,18 +52,26 @@ MDXFile (filename, buf)
{
unsigned char buffer[BUFSIZ];
MDX_CTX ctx;
- int f,i,j;
+ int f, i, j;
- MDXInit(&ctx);
- f = open(filename, O_RDONLY);
- if (f < 0) return 0;
- while ((i = read(f,buffer,sizeof buffer)) > 0) {
- MDXUpdate(&ctx,buffer,i);
+ /* directories return a 'useless' constant value anyway,
+ might as well return NULL */
+ struct stat fsbuf;
+
+ if (stat(filename, &fsbuf) || (fsbuf.st_mode & S_IFDIR)) {
+ errno = EISDIR;
+ return NULL;
}
+
+ MDXInit(&ctx);
+ if ((f = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
+ return NULL;;
+ while ((i = read(f, buffer, sizeof buffer)) > 0)
+ MDXUpdate(&ctx, buffer, i);
j = errno;
close(f);
errno = j;
- if (i < 0) return 0;
+ if (i < 0) return NULL;
return MDXEnd(&ctx, buf);
}
--
Network Security Technologies Inc. - Commercial support for OpenBSD
www.netsec.net matthew.patton@netsec.net
"Government is not reason; it is not eloquence; it is force!
Like fire, it is a dangerous servant and a fearful master."
- George Washington