@@ -1508,12 +1508,24 @@ static void free_bprm(struct linux_binprm *bprm)
1508
1508
kfree (bprm );
1509
1509
}
1510
1510
1511
- static struct linux_binprm * alloc_bprm (int fd , struct filename * filename )
1511
+ static struct linux_binprm * alloc_bprm (int fd , struct filename * filename , int flags )
1512
1512
{
1513
- struct linux_binprm * bprm = kzalloc (sizeof (* bprm ), GFP_KERNEL );
1513
+ struct linux_binprm * bprm ;
1514
+ struct file * file ;
1514
1515
int retval = - ENOMEM ;
1515
- if (!bprm )
1516
- goto out ;
1516
+
1517
+ file = do_open_execat (fd , filename , flags );
1518
+ if (IS_ERR (file ))
1519
+ return ERR_CAST (file );
1520
+
1521
+ bprm = kzalloc (sizeof (* bprm ), GFP_KERNEL );
1522
+ if (!bprm ) {
1523
+ allow_write_access (file );
1524
+ fput (file );
1525
+ return ERR_PTR (- ENOMEM );
1526
+ }
1527
+
1528
+ bprm -> file = file ;
1517
1529
1518
1530
if (fd == AT_FDCWD || filename -> name [0 ] == '/' ) {
1519
1531
bprm -> filename = filename -> name ;
@@ -1526,18 +1538,28 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename)
1526
1538
if (!bprm -> fdpath )
1527
1539
goto out_free ;
1528
1540
1541
+ /*
1542
+ * Record that a name derived from an O_CLOEXEC fd will be
1543
+ * inaccessible after exec. This allows the code in exec to
1544
+ * choose to fail when the executable is not mmaped into the
1545
+ * interpreter and an open file descriptor is not passed to
1546
+ * the interpreter. This makes for a better user experience
1547
+ * than having the interpreter start and then immediately fail
1548
+ * when it finds the executable is inaccessible.
1549
+ */
1550
+ if (get_close_on_exec (fd ))
1551
+ bprm -> interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE ;
1552
+
1529
1553
bprm -> filename = bprm -> fdpath ;
1530
1554
}
1531
1555
bprm -> interp = bprm -> filename ;
1532
1556
1533
1557
retval = bprm_mm_init (bprm );
1534
- if (retval )
1535
- goto out_free ;
1536
- return bprm ;
1558
+ if (!retval )
1559
+ return bprm ;
1537
1560
1538
1561
out_free :
1539
1562
free_bprm (bprm );
1540
- out :
1541
1563
return ERR_PTR (retval );
1542
1564
}
1543
1565
@@ -1807,10 +1829,8 @@ static int exec_binprm(struct linux_binprm *bprm)
1807
1829
/*
1808
1830
* sys_execve() executes a new program.
1809
1831
*/
1810
- static int bprm_execve (struct linux_binprm * bprm ,
1811
- int fd , struct filename * filename , int flags )
1832
+ static int bprm_execve (struct linux_binprm * bprm )
1812
1833
{
1813
- struct file * file ;
1814
1834
int retval ;
1815
1835
1816
1836
retval = prepare_bprm_creds (bprm );
@@ -1826,26 +1846,8 @@ static int bprm_execve(struct linux_binprm *bprm,
1826
1846
current -> in_execve = 1 ;
1827
1847
sched_mm_cid_before_execve (current );
1828
1848
1829
- file = do_open_execat (fd , filename , flags );
1830
- retval = PTR_ERR (file );
1831
- if (IS_ERR (file ))
1832
- goto out_unmark ;
1833
-
1834
1849
sched_exec ();
1835
1850
1836
- bprm -> file = file ;
1837
- /*
1838
- * Record that a name derived from an O_CLOEXEC fd will be
1839
- * inaccessible after exec. This allows the code in exec to
1840
- * choose to fail when the executable is not mmaped into the
1841
- * interpreter and an open file descriptor is not passed to
1842
- * the interpreter. This makes for a better user experience
1843
- * than having the interpreter start and then immediately fail
1844
- * when it finds the executable is inaccessible.
1845
- */
1846
- if (bprm -> fdpath && get_close_on_exec (fd ))
1847
- bprm -> interp_flags |= BINPRM_FLAGS_PATH_INACCESSIBLE ;
1848
-
1849
1851
/* Set the unchanging part of bprm->cred */
1850
1852
retval = security_bprm_creds_for_exec (bprm );
1851
1853
if (retval )
@@ -1875,7 +1877,6 @@ static int bprm_execve(struct linux_binprm *bprm,
1875
1877
if (bprm -> point_of_no_return && !fatal_signal_pending (current ))
1876
1878
force_fatal_sig (SIGSEGV );
1877
1879
1878
- out_unmark :
1879
1880
sched_mm_cid_after_execve (current );
1880
1881
current -> fs -> in_exec = 0 ;
1881
1882
current -> in_execve = 0 ;
@@ -1910,7 +1911,7 @@ static int do_execveat_common(int fd, struct filename *filename,
1910
1911
* further execve() calls fail. */
1911
1912
current -> flags &= ~PF_NPROC_EXCEEDED ;
1912
1913
1913
- bprm = alloc_bprm (fd , filename );
1914
+ bprm = alloc_bprm (fd , filename , flags );
1914
1915
if (IS_ERR (bprm )) {
1915
1916
retval = PTR_ERR (bprm );
1916
1917
goto out_ret ;
@@ -1959,7 +1960,7 @@ static int do_execveat_common(int fd, struct filename *filename,
1959
1960
bprm -> argc = 1 ;
1960
1961
}
1961
1962
1962
- retval = bprm_execve (bprm , fd , filename , flags );
1963
+ retval = bprm_execve (bprm );
1963
1964
out_free :
1964
1965
free_bprm (bprm );
1965
1966
@@ -1984,7 +1985,7 @@ int kernel_execve(const char *kernel_filename,
1984
1985
if (IS_ERR (filename ))
1985
1986
return PTR_ERR (filename );
1986
1987
1987
- bprm = alloc_bprm (fd , filename );
1988
+ bprm = alloc_bprm (fd , filename , 0 );
1988
1989
if (IS_ERR (bprm )) {
1989
1990
retval = PTR_ERR (bprm );
1990
1991
goto out_ret ;
@@ -2019,7 +2020,7 @@ int kernel_execve(const char *kernel_filename,
2019
2020
if (retval < 0 )
2020
2021
goto out_free ;
2021
2022
2022
- retval = bprm_execve (bprm , fd , filename , 0 );
2023
+ retval = bprm_execve (bprm );
2023
2024
out_free :
2024
2025
free_bprm (bprm );
2025
2026
out_ret :
0 commit comments