Index: uspace/lib/c/generic/dirent.c
===================================================================
--- uspace/lib/c/generic/dirent.c	(revision df4f7c30d6470268a01ea5ec78aa0cacf506662d)
+++ uspace/lib/c/generic/dirent.c	(revision 128a311e02aa7ad74377185bd929a82874cfcade)
@@ -47,10 +47,6 @@
 };
 
-/** Open directory by its handle (a.k.a. file descriptor).
- *
- * @param handle Directory handle
- * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
- */
-DIR *opendir_handle(int handle)
+/** Like opendir_handle, but takes ownership of the handle if successful. */
+static DIR *opendir_internal(int handle)
 {
 	DIR *dirp = malloc(sizeof(DIR));
@@ -72,4 +68,29 @@
 }
 
+/** Open directory by its handle (a.k.a. file descriptor).
+ *
+ * @param handle Directory handle
+ * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
+ */
+DIR *opendir_handle(int handle)
+{
+	int my_handle;
+	errno_t rc = vfs_clone(handle, -1, false, &my_handle); // Clone the file handle, otherwise closedir would put the
+	                                                       // handle that was passed to us here by the caller and that we don't own.
+	if (rc != EOK) {
+		errno = rc;
+		return NULL;
+	}
+
+	DIR *dirp = opendir_internal(my_handle);
+	rc = errno;
+	if (rc != EOK) {
+		vfs_put(my_handle);
+		errno = rc;
+		return NULL;
+	}
+	return dirp;
+}
+
 /** Open directory by its pathname.
  *
@@ -87,5 +108,5 @@
 	}
 
-	DIR *dirp = opendir_handle(fd);
+	DIR *dirp = opendir_internal(fd);
 	rc = errno;
 	if (rc != EOK) {
@@ -97,5 +118,5 @@
 }
 
-/** Read directory entry.
+/** Read current directory entry and advance to the next one.
  *
  * @param dirp Open directory
