Skip to content

Commit 540a412

Browse files
author
Cristy
committed
1 parent bf920b9 commit 540a412

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

config/policy-secure.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@
108108
<!-- Set the maximum amount of memory in bytes that are permitted for
109109
allocation requests. -->
110110
<policy domain="system" name="max-memory-request" value="256MiB"/>
111+
<!-- If the basename of path is a symbolic link, the open fails -->
112+
<policy domain="system" name="symlink" rights="none" pattern="follow"/>
111113
</policymap>

magick/blob.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,19 +1160,36 @@ MagickExport unsigned char *FileToBlob(const char *filename,const size_t extent,
11601160
file=fileno(stdin);
11611161
if (LocaleCompare(filename,"-") != 0)
11621162
{
1163+
int
1164+
flags = O_RDONLY | O_BINARY;
1165+
11631166
status=GetPathAttributes(filename,&attributes);
11641167
if ((status == MagickFalse) || (S_ISDIR(attributes.st_mode) != 0))
11651168
{
11661169
ThrowFileException(exception,BlobError,"UnableToReadBlob",filename);
11671170
return(NULL);
11681171
}
1169-
file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1172+
#if defined(O_NOFOLLOW)
1173+
status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,"follow");
1174+
if (status == MagickFalse)
1175+
flags|=O_NOFOLLOW;
1176+
#endif
1177+
file=open_utf8(filename,flags,0);
11701178
}
11711179
if (file == -1)
11721180
{
11731181
ThrowFileException(exception,BlobError,"UnableToOpenFile",filename);
11741182
return((unsigned char *) NULL);
11751183
}
1184+
status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1185+
if (status == MagickFalse)
1186+
{
1187+
file=close_utf8(file)-1;
1188+
errno=EPERM;
1189+
(void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1190+
"NotAuthorized","`%s'",filename);
1191+
return(NULL);
1192+
}
11761193
offset=(MagickOffsetType) lseek(file,0,SEEK_END);
11771194
count=0;
11781195
if ((file == fileno(stdin)) || (offset < 0) ||
@@ -1366,7 +1383,7 @@ MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
13661383
assert(filename != (const char *) NULL);
13671384
if (IsEventLogging() != MagickFalse)
13681385
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1369-
status=IsRightsAuthorized(PathPolicyDomain,WritePolicyRights,filename);
1386+
status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
13701387
if (status == MagickFalse)
13711388
{
13721389
errno=EPERM;
@@ -1376,13 +1393,31 @@ MagickExport MagickBooleanType FileToImage(Image *image,const char *filename)
13761393
}
13771394
file=fileno(stdin);
13781395
if (LocaleCompare(filename,"-") != 0)
1379-
file=open_utf8(filename,O_RDONLY | O_BINARY,0);
1396+
{
1397+
int
1398+
flags = O_RDONLY | O_BINARY;
1399+
1400+
#if defined(O_NOFOLLOW)
1401+
status=IsRightsAuthorized(SystemPolicyDomain,ReadPolicyRights,"follow");
1402+
if (status == MagickFalse)
1403+
flags|=O_NOFOLLOW;
1404+
#endif
1405+
file=open_utf8(filename,flags,0);
1406+
}
13801407
if (file == -1)
13811408
{
13821409
ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob",
13831410
filename);
13841411
return(MagickFalse);
13851412
}
1413+
status=IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,filename);
1414+
if (status == MagickFalse)
1415+
{
1416+
errno=EPERM;
1417+
(void) ThrowMagickException(&image->exception,GetMagickModule(),
1418+
PolicyError,"NotAuthorized","`%s'",filename);
1419+
return(MagickFalse);
1420+
}
13861421
quantum=(size_t) MagickMaxBufferExtent;
13871422
if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
13881423
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
@@ -2977,6 +3012,13 @@ MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
29773012
(void) SetStreamBuffering(image_info,image);
29783013
}
29793014
}
3015+
if (IsRightsAuthorized(PathPolicyDomain,rights,filename) == MagickFalse)
3016+
{
3017+
errno=EPERM;
3018+
(void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
3019+
"NotAuthorized","`%s'",filename);
3020+
return(MagickFalse);
3021+
}
29803022
blob_info->status=0;
29813023
blob_info->error_number=0;
29823024
if (blob_info->type != UndefinedStream)

0 commit comments

Comments
 (0)