1- // Copyright lowRISC contributors.
21// Licensed under the Apache License, Version 2.0, see LICENSE for details.
32// SPDX-License-Identifier: Apache-2.0
43
@@ -17,7 +16,7 @@ using namespace CHERI;
1716using namespace sonata ::lcd;
1817
1918constexpr bool DebugDemo = true ;
20- constexpr uint32_t LengthScrollMillis = 350u ;
19+ constexpr uint32_t LengthScrollMillis = 150u ;
2120constexpr Color BackgroundColor = Color::Black;
2221constexpr Color ForegroundColor = Color::White;
2322
@@ -141,24 +140,28 @@ bool length_joystick_control(volatile SonataGpioBoard *gpio, size_t *current)
141140 * @param actual_req_len The genuine length of the `bufferMessage`. This MUST
142141 * match, or is undefined behaviour.
143142 */
144- void initial_lcd_write (SonataLcd *lcd,
145- const char *bufferMessage,
146- size_t actual_req_len)
143+ void initial_lcd_write (SonataLcd *lcd)
147144{
148145 lcd->draw_str ({5 , 5 },
149146 " Move Joystick to Change Length." ,
150147 BackgroundColor,
151- ForegroundColor);
152- lcd->draw_str (
153- {5 , 15 }, " Press Joystick to Send." , BackgroundColor, ForegroundColor);
154- lcd->draw_str (
155- {5 , 30 }, " Buffer Message: " , BackgroundColor, ForegroundColor);
156- char displayMessage[actual_req_len + 1 ];
157- memcpy (displayMessage, bufferMessage, actual_req_len);
158- displayMessage[actual_req_len] = ' \0 ' ;
159- lcd->draw_str ({70 , 30 }, displayMessage, BackgroundColor, ForegroundColor);
160- lcd->draw_str (
161- {5 , 40 }, " Request Length: " , BackgroundColor, ForegroundColor);
148+ ForegroundColor,
149+ Font::m5x7_16pt);
150+ lcd->draw_str ({5 , 15 },
151+ " Press Joystick to Send." ,
152+ BackgroundColor,
153+ ForegroundColor,
154+ Font::m5x7_16pt);
155+ lcd->draw_str ({5 , 30 },
156+ " Request a larger buffer " ,
157+ BackgroundColor,
158+ ForegroundColor,
159+ Font::m5x7_16pt);
160+ lcd->draw_str ({5 , 40 },
161+ " Suggested Length: " ,
162+ BackgroundColor,
163+ ForegroundColor,
164+ Font::m5x7_16pt);
162165}
163166
164167/* *
@@ -175,7 +178,8 @@ void draw_request_length(SonataLcd *lcd,
175178 constexpr size_t ReqLenStrLen = 15u ;
176179 char req_len_s[ReqLenStrLen];
177180 size_t_to_str_base10 (req_len_s, request_length);
178- lcd->draw_str ({70 , 40 }, req_len_s, BackgroundColor, ForegroundColor);
181+ lcd->draw_str (
182+ {110 , 40 }, req_len_s, BackgroundColor, ForegroundColor, Font::m5x7_16pt);
179183}
180184
181185/* *
@@ -206,67 +210,75 @@ void get_request_length(SonataLcd *lcd,
206210 {
207211 continue ; // Only re-draw when changed
208212 }
209- lcd->draw_str ({70 , 40 }, " " , BackgroundColor, ForegroundColor);
213+ lcd->draw_str ({110 , 40 },
214+ " " ,
215+ BackgroundColor,
216+ ForegroundColor,
217+ Font::m5x7_16pt);
210218 draw_request_length (lcd, gpio, *request_length);
211219 }
212220
213221 Debug::log (" Heartbeat submitted with length {}" , (int )(*request_length));
214222}
215223
216224/* *
217- * @brief This function contains the logic for writing the heartbeat/bleed
218- * response message to the LCD, breaking the message across lines where it is
219- * necessary.
225+ * @brief This function mocks the network and show the package on the lcd
226+ * instead.
220227 *
221- * @param lcd The Sonata LCD driver to use.
222- * @param request_length The length of the heartbeat request. May not be the
223- * actual message length.
224- * @param result The heartbeat response to reply with. This should not be
225- * null-terminated.
228+ * @param lcd A handle to Sonata's LCD
229+ * @param package The package to be sent.
230+ * @param len The length of the package .
226231 */
227- void draw_heartbleed_response (SonataLcd *lcd,
228- size_t request_length,
229- const char *result)
232+ void network_send (void *handle, const char *package, size_t len)
230233{
231- constexpr uint32_t CharsPerLine = 50u ;
234+ constexpr uint32_t CharsPerLine = 29 ;
235+ SonataLcd *lcd = (SonataLcd *)handle;
232236
233- // Format the result message for LCD display as a null-terminated string.
234- constexpr size_t PrefixLen = 10u ;
235- constexpr char ResponsePrefix[PrefixLen + 1 ] = " Response: " ;
236- char result_s[PrefixLen + request_length + 1 ];
237- memcpy (result_s, ResponsePrefix, PrefixLen);
238- memcpy (&result_s[PrefixLen], result, request_length);
239- result_s[PrefixLen + request_length] = ' \0 ' ;
237+ size_t w_boarder = 2 ;
238+ lcd->fill_rect ({w_boarder, 50 , 160 - w_boarder, 128 }, Color::Grey);
240239
241240 // Break the result message into several lines if it is too long to fit on
242241 // one line.
243- char result_line [CharsPerLine + 1 ];
244- const char *result_char = result_s ;
242+ char line_content [CharsPerLine + 1 ];
243+ const char *cursor = package ;
245244 size_t line_length = 0u ;
246245 size_t line_num = 0u ;
247- while (*result_char != ' \0 ' )
246+ while (len-- != 0 )
248247 {
249- result_line[line_length++] = *result_char;
250- result_char++;
248+ if (*cursor == 0 )
249+ {
250+ line_content[line_length++] = ' `' ;
251+ }
252+ else if (isprint ((int )(*cursor)) == 0 )
253+ {
254+ line_content[line_length++] = ' %' ;
255+ }
256+ else
257+ {
258+ line_content[line_length++] = *cursor;
259+ }
260+ cursor++;
251261 if (line_length == CharsPerLine)
252262 {
253- result_line [line_length] = ' \0 ' ;
263+ line_content [line_length] = ' \0 ' ;
254264 lcd->draw_str ({5 , 55 + 10 * line_num},
255- result_line,
256- BackgroundColor,
257- ForegroundColor);
265+ line_content,
266+ Color::Grey,
267+ Color::Black,
268+ Font::m5x7_16pt);
258269 line_length = 0 ;
259270 line_num++;
260271 }
261272 }
262273 // Write the final line containing the remainder of the message.
263274 if (line_length)
264275 {
265- result_line [line_length] = ' \0 ' ;
276+ line_content [line_length] = ' \0 ' ;
266277 lcd->draw_str ({5 , 55 + 10 * line_num},
267- result_line,
268- BackgroundColor,
269- ForegroundColor);
278+ line_content,
279+ Color::Grey,
280+ Color::Black,
281+ Font::m5x7_16pt);
270282 }
271283}
272284
@@ -277,24 +289,35 @@ void draw_heartbleed_response(SonataLcd *lcd,
277289 Size displaySize = lcd->resolution ();
278290 Point centre = {displaySize.width / 2 , displaySize.height / 2 };
279291 lcd->clean (BackgroundColor);
292+ size_t w_boarder = 2 ;
293+ lcd->fill_rect ({w_boarder, 50 , 160 - w_boarder, 128 }, Color::Grey);
280294
281295 // Initialise GPIO driver to interact with the Joystick
282296 auto gpio = MMIO_CAPABILITY (SonataGpioBoard, gpio_board);
283297
284- // We represent the heartbeat message as a small array of incoming bytes,
285- // and initialise with its actual size.
286- constexpr size_t actual_req_len = 4 ;
287- const char bufferMessage[actual_req_len] = {' B' , ' i' , ' r' , ' d' };
288-
289- size_t req_len = actual_req_len;
298+ size_t req_len = 8 ;
290299 while (true )
291300 {
292- initial_lcd_write (lcd, bufferMessage, actual_req_len);
301+ // We allocate a big chunck of memory to temporary store a json file
302+ // with sensitive information. Then we free the buffer without cleaning
303+ // the content.
304+ const size_t DbSize = 128 ;
305+ char *ptr = (char *)malloc (DbSize);
306+ read_file (" clients.db" , ptr, DbSize);
307+ free (ptr);
308+
309+ initial_lcd_write (lcd);
310+
311+ // Wait for the request.
293312 get_request_length (lcd, gpio, &req_len);
294- const char *result = heartbleed (bufferMessage, req_len);
295- lcd->fill_rect ({5 , 55 , displaySize.width , displaySize.height },
296- BackgroundColor);
297- draw_heartbleed_response (lcd, req_len, result);
313+
314+ const char *result =
315+ run_query (" SELECT name FROM animal WHERE can_fly=yes LIMIT 1" );
316+
317+ // We send back the response to the request without checking that the
318+ // requested length exeeds the needed size, which can leek information.
319+ heartbleed (lcd, result, req_len);
320+
298321 free ((void *)result);
299322 }
300323
0 commit comments