@@ -904,6 +904,10 @@ EXPORT_SYMBOL(transfer_args_to_stack);
904
904
905
905
#endif /* CONFIG_MMU */
906
906
907
+ /*
908
+ * On success, caller must call do_close_execat() on the returned
909
+ * struct file to close it.
910
+ */
907
911
static struct file * do_open_execat (int fd , struct filename * name , int flags )
908
912
{
909
913
struct file * file ;
@@ -948,6 +952,17 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
948
952
return ERR_PTR (err );
949
953
}
950
954
955
+ /**
956
+ * open_exec - Open a path name for execution
957
+ *
958
+ * @name: path name to open with the intent of executing it.
959
+ *
960
+ * Returns ERR_PTR on failure or allocated struct file on success.
961
+ *
962
+ * As this is a wrapper for the internal do_open_execat(), callers
963
+ * must call allow_write_access() before fput() on release. Also see
964
+ * do_close_execat().
965
+ */
951
966
struct file * open_exec (const char * name )
952
967
{
953
968
struct filename * filename = getname_kernel (name );
@@ -1409,6 +1424,9 @@ int begin_new_exec(struct linux_binprm * bprm)
1409
1424
1410
1425
out_unlock :
1411
1426
up_write (& me -> signal -> exec_update_lock );
1427
+ if (!bprm -> cred )
1428
+ mutex_unlock (& me -> signal -> cred_guard_mutex );
1429
+
1412
1430
out :
1413
1431
return retval ;
1414
1432
}
@@ -1484,6 +1502,15 @@ static int prepare_bprm_creds(struct linux_binprm *bprm)
1484
1502
return - ENOMEM ;
1485
1503
}
1486
1504
1505
+ /* Matches do_open_execat() */
1506
+ static void do_close_execat (struct file * file )
1507
+ {
1508
+ if (!file )
1509
+ return ;
1510
+ allow_write_access (file );
1511
+ fput (file );
1512
+ }
1513
+
1487
1514
static void free_bprm (struct linux_binprm * bprm )
1488
1515
{
1489
1516
if (bprm -> mm ) {
@@ -1495,10 +1522,7 @@ static void free_bprm(struct linux_binprm *bprm)
1495
1522
mutex_unlock (& current -> signal -> cred_guard_mutex );
1496
1523
abort_creds (bprm -> cred );
1497
1524
}
1498
- if (bprm -> file ) {
1499
- allow_write_access (bprm -> file );
1500
- fput (bprm -> file );
1501
- }
1525
+ do_close_execat (bprm -> file );
1502
1526
if (bprm -> executable )
1503
1527
fput (bprm -> executable );
1504
1528
/* If a binfmt changed the interp, free it. */
@@ -1520,8 +1544,7 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl
1520
1544
1521
1545
bprm = kzalloc (sizeof (* bprm ), GFP_KERNEL );
1522
1546
if (!bprm ) {
1523
- allow_write_access (file );
1524
- fput (file );
1547
+ do_close_execat (file );
1525
1548
return ERR_PTR (- ENOMEM );
1526
1549
}
1527
1550
@@ -1610,6 +1633,7 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
1610
1633
}
1611
1634
rcu_read_unlock ();
1612
1635
1636
+ /* "users" and "in_exec" locked for copy_fs() */
1613
1637
if (p -> fs -> users > n_fs )
1614
1638
bprm -> unsafe |= LSM_UNSAFE_SHARE ;
1615
1639
else
@@ -1826,9 +1850,6 @@ static int exec_binprm(struct linux_binprm *bprm)
1826
1850
return 0 ;
1827
1851
}
1828
1852
1829
- /*
1830
- * sys_execve() executes a new program.
1831
- */
1832
1853
static int bprm_execve (struct linux_binprm * bprm )
1833
1854
{
1834
1855
int retval ;
0 commit comments