Skip to content

Commit 829a6f2

Browse files
Jeff BrownThe Android Automerger
authored andcommitted
Fix a leak in Parcel::writeBlob.
Was mistakenly assuming that Parcel::writeFileDescriptor took ownership of the fd that was passed in. It does not! Added some comments and a default parameter to allow the caller to specify whether it wishes the Parcel to take ownership. Bug: 5563374 Change-Id: I5a12f51d582bf246ce90133cce7690bb9bca93f6
1 parent 6d25e34 commit 829a6f2

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

include/binder/Parcel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ class Parcel
110110

111111
// Place a file descriptor into the parcel. The given fd must remain
112112
// valid for the lifetime of the parcel.
113-
status_t writeFileDescriptor(int fd);
113+
// The Parcel does not take ownership of the given fd unless you ask it to.
114+
status_t writeFileDescriptor(int fd, bool takeOwnership = false);
114115

115116
// Place a file descriptor into the parcel. A dup of the fd is made, which
116117
// will be closed once the parcel is destroyed.

libs/binder/Parcel.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -710,24 +710,19 @@ status_t Parcel::writeNativeHandle(const native_handle* handle)
710710
return err;
711711
}
712712

713-
status_t Parcel::writeFileDescriptor(int fd)
713+
status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership)
714714
{
715715
flat_binder_object obj;
716716
obj.type = BINDER_TYPE_FD;
717717
obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
718718
obj.handle = fd;
719-
obj.cookie = (void*)0;
719+
obj.cookie = (void*) (takeOwnership ? 1 : 0);
720720
return writeObject(obj, true);
721721
}
722722

723723
status_t Parcel::writeDupFileDescriptor(int fd)
724724
{
725-
flat_binder_object obj;
726-
obj.type = BINDER_TYPE_FD;
727-
obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
728-
obj.handle = dup(fd);
729-
obj.cookie = (void*)1;
730-
return writeObject(obj, true);
725+
return writeFileDescriptor(dup(fd), true /*takeOwnership*/);
731726
}
732727

733728
status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
@@ -764,7 +759,7 @@ status_t Parcel::writeBlob(size_t len, WritableBlob* outBlob)
764759
} else {
765760
status = writeInt32(1);
766761
if (!status) {
767-
status = writeFileDescriptor(fd);
762+
status = writeFileDescriptor(fd, true /*takeOwnership*/);
768763
if (!status) {
769764
outBlob->init(true /*mapped*/, ptr, len);
770765
return NO_ERROR;

0 commit comments

Comments
 (0)