@@ -155,6 +155,9 @@ module_param_call(stop_on_user_error, binder_set_stop_on_user_error,
155155#define to_binder_buffer_object (hdr ) \
156156 container_of(hdr, struct binder_buffer_object, hdr)
157157
158+ #define to_binder_fd_array_object (hdr ) \
159+ container_of(hdr, struct binder_fd_array_object, hdr)
160+
158161enum binder_stat_types {
159162 BINDER_STAT_PROC ,
160163 BINDER_STAT_THREAD ,
@@ -1310,6 +1313,9 @@ static size_t binder_validate_object(struct binder_buffer *buffer, u64 offset)
13101313 case BINDER_TYPE_PTR :
13111314 object_size = sizeof (struct binder_buffer_object );
13121315 break ;
1316+ case BINDER_TYPE_FDA :
1317+ object_size = sizeof (struct binder_fd_array_object );
1318+ break ;
13131319 default :
13141320 return 0 ;
13151321 }
@@ -1503,6 +1509,47 @@ static void binder_transaction_buffer_release(struct binder_proc *proc,
15031509 * transaction buffer gets freed
15041510 */
15051511 break ;
1512+ case BINDER_TYPE_FDA : {
1513+ struct binder_fd_array_object * fda ;
1514+ struct binder_buffer_object * parent ;
1515+ uintptr_t parent_buffer ;
1516+ u32 * fd_array ;
1517+ size_t fd_index ;
1518+ binder_size_t fd_buf_size ;
1519+
1520+ fda = to_binder_fd_array_object (hdr );
1521+ parent = binder_validate_ptr (buffer , fda -> parent ,
1522+ off_start ,
1523+ offp - off_start );
1524+ if (!parent ) {
1525+ pr_err ("transaction release %d bad parent offset" ,
1526+ debug_id );
1527+ continue ;
1528+ }
1529+ /*
1530+ * Since the parent was already fixed up, convert it
1531+ * back to kernel address space to access it
1532+ */
1533+ parent_buffer = parent -> buffer -
1534+ proc -> user_buffer_offset ;
1535+
1536+ fd_buf_size = sizeof (u32 ) * fda -> num_fds ;
1537+ if (fda -> num_fds >= SIZE_MAX / sizeof (u32 )) {
1538+ pr_err ("transaction release %d invalid number of fds (%lld)\n" ,
1539+ debug_id , (u64 )fda -> num_fds );
1540+ continue ;
1541+ }
1542+ if (fd_buf_size > parent -> length ||
1543+ fda -> parent_offset > parent -> length - fd_buf_size ) {
1544+ /* No space for all file descriptors here. */
1545+ pr_err ("transaction release %d not enough space for %lld fds in buffer\n" ,
1546+ debug_id , (u64 )fda -> num_fds );
1547+ continue ;
1548+ }
1549+ fd_array = (u32 * )(parent_buffer + fda -> parent_offset );
1550+ for (fd_index = 0 ; fd_index < fda -> num_fds ; fd_index ++ )
1551+ task_close_fd (proc , fd_array [fd_index ]);
1552+ } break ;
15061553 default :
15071554 pr_err ("transaction release %d bad object type %x\n" ,
15081555 debug_id , hdr -> type );
@@ -1672,6 +1719,63 @@ static int binder_translate_fd(int fd,
16721719 return ret ;
16731720}
16741721
1722+ static int binder_translate_fd_array (struct binder_fd_array_object * fda ,
1723+ struct binder_buffer_object * parent ,
1724+ struct binder_transaction * t ,
1725+ struct binder_thread * thread ,
1726+ struct binder_transaction * in_reply_to )
1727+ {
1728+ binder_size_t fdi , fd_buf_size , num_installed_fds ;
1729+ int target_fd ;
1730+ uintptr_t parent_buffer ;
1731+ u32 * fd_array ;
1732+ struct binder_proc * proc = thread -> proc ;
1733+ struct binder_proc * target_proc = t -> to_proc ;
1734+
1735+ fd_buf_size = sizeof (u32 ) * fda -> num_fds ;
1736+ if (fda -> num_fds >= SIZE_MAX / sizeof (u32 )) {
1737+ binder_user_error ("%d:%d got transaction with invalid number of fds (%lld)\n" ,
1738+ proc -> pid , thread -> pid , (u64 )fda -> num_fds );
1739+ return - EINVAL ;
1740+ }
1741+ if (fd_buf_size > parent -> length ||
1742+ fda -> parent_offset > parent -> length - fd_buf_size ) {
1743+ /* No space for all file descriptors here. */
1744+ binder_user_error ("%d:%d not enough space to store %lld fds in buffer\n" ,
1745+ proc -> pid , thread -> pid , (u64 )fda -> num_fds );
1746+ return - EINVAL ;
1747+ }
1748+ /*
1749+ * Since the parent was already fixed up, convert it
1750+ * back to the kernel address space to access it
1751+ */
1752+ parent_buffer = parent -> buffer - target_proc -> user_buffer_offset ;
1753+ fd_array = (u32 * )(parent_buffer + fda -> parent_offset );
1754+ if (!IS_ALIGNED ((unsigned long )fd_array , sizeof (u32 ))) {
1755+ binder_user_error ("%d:%d parent offset not aligned correctly.\n" ,
1756+ proc -> pid , thread -> pid );
1757+ return - EINVAL ;
1758+ }
1759+ for (fdi = 0 ; fdi < fda -> num_fds ; fdi ++ ) {
1760+ target_fd = binder_translate_fd (fd_array [fdi ], t , thread ,
1761+ in_reply_to );
1762+ if (target_fd < 0 )
1763+ goto err_translate_fd_failed ;
1764+ fd_array [fdi ] = target_fd ;
1765+ }
1766+ return 0 ;
1767+
1768+ err_translate_fd_failed :
1769+ /*
1770+ * Failed to allocate fd or security error, free fds
1771+ * installed so far.
1772+ */
1773+ num_installed_fds = fdi ;
1774+ for (fdi = 0 ; fdi < num_installed_fds ; fdi ++ )
1775+ task_close_fd (target_proc , fd_array [fdi ]);
1776+ return target_fd ;
1777+ }
1778+
16751779static int binder_fixup_parent (struct binder_transaction * t ,
16761780 struct binder_thread * thread ,
16771781 struct binder_buffer_object * bp ,
@@ -2000,6 +2104,38 @@ static void binder_transaction(struct binder_proc *proc,
20002104 fp -> pad_binder = 0 ;
20012105 fp -> fd = target_fd ;
20022106 } break ;
2107+ case BINDER_TYPE_FDA : {
2108+ struct binder_fd_array_object * fda =
2109+ to_binder_fd_array_object (hdr );
2110+ struct binder_buffer_object * parent =
2111+ binder_validate_ptr (t -> buffer , fda -> parent ,
2112+ off_start ,
2113+ offp - off_start );
2114+ if (!parent ) {
2115+ binder_user_error ("%d:%d got transaction with invalid parent offset or type\n" ,
2116+ proc -> pid , thread -> pid );
2117+ return_error = BR_FAILED_REPLY ;
2118+ goto err_bad_parent ;
2119+ }
2120+ if (!binder_validate_fixup (t -> buffer , off_start ,
2121+ parent , fda -> parent_offset ,
2122+ last_fixup_obj ,
2123+ last_fixup_min_off )) {
2124+ binder_user_error ("%d:%d got transaction with out-of-order buffer fixup\n" ,
2125+ proc -> pid , thread -> pid );
2126+ return_error = BR_FAILED_REPLY ;
2127+ goto err_bad_parent ;
2128+ }
2129+ ret = binder_translate_fd_array (fda , parent , t , thread ,
2130+ in_reply_to );
2131+ if (ret < 0 ) {
2132+ return_error = BR_FAILED_REPLY ;
2133+ goto err_translate_failed ;
2134+ }
2135+ last_fixup_obj = parent ;
2136+ last_fixup_min_off =
2137+ fda -> parent_offset + sizeof (u32 ) * fda -> num_fds ;
2138+ } break ;
20032139 case BINDER_TYPE_PTR : {
20042140 struct binder_buffer_object * bp =
20052141 to_binder_buffer_object (hdr );
@@ -2070,6 +2206,7 @@ static void binder_transaction(struct binder_proc *proc,
20702206err_translate_failed :
20712207err_bad_object_type :
20722208err_bad_offset :
2209+ err_bad_parent :
20732210err_copy_data_failed :
20742211 trace_binder_transaction_failed_buffer_release (t -> buffer );
20752212 binder_transaction_buffer_release (target_proc , t -> buffer , offp );
0 commit comments