Skip to content

Commit cea2770

Browse files
committed
Merge branch 'PHP-5.6' into PHP-7.0
* PHP-5.6: Fix bug #77022 - use file mode or umask for new files
2 parents 87bf84c + 69f5e79 commit cea2770

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ PHP NEWS
66
. Fixed bug #77153 (imap_open allows to run arbitrary shell commands via
77
mailbox parameter). (Stas)
88

9+
- Phar:
10+
. Fixed bug #77022 (PharData always creates new files with mode 0666). (Stas)
11+
912
13 Sep 2018 PHP 7.0.32
1013

1114
- Apache2

ext/phar/phar_object.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,8 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, int filenam
36633663
char *error;
36643664
size_t contents_len;
36653665
phar_entry_data *data;
3666-
php_stream *contents_file;
3666+
php_stream *contents_file = NULL;
3667+
php_stream_statbuf ssb;
36673668

36683669
if (filename_len >= sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1) && (filename[5] == '/' || filename[5] == '\\' || filename[5] == '\0')) {
36693670
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory", (*pphar)->fname);
@@ -3697,10 +3698,20 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, int filenam
36973698
}
36983699
php_stream_copy_to_stream_ex(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
36993700
}
3700-
37013701
data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
37023702
}
37033703

3704+
if (contents_file != NULL && php_stream_stat(contents_file, &ssb) != -1) {
3705+
data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
3706+
} else {
3707+
#ifndef _WIN32
3708+
mode_t mask;
3709+
mask = umask(0);
3710+
umask(mask);
3711+
data->internal_file->flags &= ~mask;
3712+
#endif
3713+
}
3714+
37043715
/* check for copy-on-write */
37053716
if (pphar[0] != data->phar) {
37063717
*pphar = data->phar;

ext/phar/tests/bug77022.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Phar: Bug #77022: PharData always creates new files with mode 0666
3+
--SKIPIF--
4+
<?php if (!extension_loaded("phar")) die("skip"); ?>
5+
--FILE--
6+
<?php
7+
umask(022);
8+
var_dump(decoct(umask()));
9+
10+
$sFile = tempnam(__DIR__, 'test77022');
11+
var_dump(decoct(stat($sFile)['mode']));
12+
13+
foreach([Phar::TAR => 'tar', Phar::ZIP => 'zip'] as $mode => $ext) {
14+
$phar = new PharData(__DIR__ . '/test77022.' . $ext, null, null, $mode);
15+
$phar->addFile($sFile, 'test-file-phar');
16+
$phar->addFromString("test-from-string", 'test-file-phar');
17+
$phar->extractTo(__DIR__);
18+
var_dump(decoct(stat(__DIR__ . '/test-file-phar')['mode']));
19+
var_dump(decoct(stat(__DIR__ . '/test-from-string')['mode']));
20+
unlink(__DIR__ . '/test-file-phar');
21+
unlink(__DIR__ . '/test-from-string');
22+
unlink(__DIR__ . '/test77022.' . $ext);
23+
}
24+
unlink($sFile);
25+
?>
26+
--EXPECT--
27+
string(2) "22"
28+
string(6) "100600"
29+
string(6) "100600"
30+
string(6) "100644"
31+
string(6) "100600"
32+
string(6) "100644"

ext/phar/tests/stat.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ phar.require_hash=1
77
phar.readonly=0
88
--FILE--
99
<?php
10+
umask(0);
1011
Phar::interceptFileFuncs();
1112
var_dump(stat(""));
1213

0 commit comments

Comments
 (0)