summaryrefslogtreecommitdiff
path: root/debian/patches/fixes0005-random-satisfy-arbitrarily-sized-reads.patch
blob: 75ed3b9f5f687bdf9453c16864b28f5acabf982a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
From df778cd01505e3a8c11f5b13f5ee26a4be290f9a Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Fri, 6 Nov 2015 14:42:04 +0100
Subject: [PATCH hurd 5/5] random: satisfy arbitrarily-sized reads

* random/random.c (trivfs_S_io_read): Satisfy arbitrarily-sized reads.
---
 random/random.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/random/random.c b/random/random.c
index f926b7f..8f76c5b 100644
--- a/random/random.c
+++ b/random/random.c
@@ -143,6 +143,11 @@ trivfs_S_io_read (struct trivfs_protid *cred,
 		  data_t *data, mach_msg_type_number_t *data_len,
 		  loff_t offs, mach_msg_type_number_t amount)
 {
+  error_t err;
+  mach_msg_type_number_t read_amount = 0;
+  void *buf = NULL;
+  size_t length;
+
   /* Deny access if they have bad credentials. */
   if (! cred)
     return EOPNOTSUPP;
@@ -151,21 +156,27 @@ trivfs_S_io_read (struct trivfs_protid *cred,
 
   pthread_mutex_lock (&global_lock);
 
-  if (amount > 0)
+  while (amount > 0)
     {
       mach_msg_type_number_t new_amount;
+      /* XXX: It would be nice to fix readable_pool to work for sizes
+	 greater than the POOLSIZE.  Otherwise we risk detecting too
+	 late that we run out of entropy and all that entropy is
+	 wasted.  */
       while (readable_pool (amount, level) == 0)
 	{
 	  if (cred->po->openmodes & O_NONBLOCK)
 	    {
 	      pthread_mutex_unlock (&global_lock);
-	      return EWOULDBLOCK;
+	      err = EWOULDBLOCK;
+	      goto errout;
 	    }
 	  read_blocked = 1;
 	  if (pthread_hurd_cond_wait_np (&wait, &global_lock))
 	    {
 	      pthread_mutex_unlock (&global_lock);
-	      return EINTR;
+	      err = EINTR;
+	      goto errout;
 	    }
 	  /* See term/users.c for possible race?  */
 	}
@@ -175,27 +186,35 @@ trivfs_S_io_read (struct trivfs_protid *cred,
 	{
 	  *data = mmap (0, amount, PROT_READ|PROT_WRITE,
 				       MAP_ANON, 0, 0);
+
 	  if (*data == MAP_FAILED)
 	    {
 	      pthread_mutex_unlock (&global_lock);
 	      return errno;
 	    }
-	}
 
-      new_amount = read_pool ((byte *) *data, amount, level);
+	  /* Keep track of our map in case of errors.  */
+	  buf = *data, length = amount;
 
-      if (new_amount < amount)
-	munmap (*data + round_page (new_amount),
-	        round_page(amount) - round_page (new_amount));
-      amount = new_amount;
+	  /* Update DATA_LEN to reflect the new buffers size.  */
+	  *data_len = amount;
+	}
+
+      new_amount = read_pool (((byte *) *data) + read_amount, amount, level);
+      read_amount += new_amount;
+      amount -= new_amount;
     }
-  *data_len = amount;
 
   /* Set atime, see term/users.c */
 
   pthread_mutex_unlock (&global_lock);
-
+  *data_len = read_amount;
   return 0;
+
+ errout:
+  if (buf)
+    munmap (buf, length);
+  return err;
 }
 
 /* Write data to an IO object.  If offset is -1, write at the object
-- 
2.1.4