Skip to main content

Rust fixme 2

  • Description: The Rust saga continues? I ask you, can I borrow that, pleeeeeaaaasseeeee?
  • Difficulty: Easy

🔎 Solution

After extracting the archive using tar -xvf fixme2.tar.gz, you’ll find the following files in the directory:

  • Cargo.toml
  • Cargo.lock
  • src/main.rs Now try compiling and running the project with cargo run, you’ll encounter the following error:
error[E0596]: cannot borrow `*borrowed_string` as mutable, as it is behind a `&` reference

This error occurs at a line like borrowed_string.push_str("...");. The issue here is that borrowed_string is an immutable reference (&String), which means you cannot modify the data it points to. The .push_str() method requires a mutable reference, because it alters the original String.

To resolve this, the function needs to accept a mutable reference (&mut String) instead. Change this function signature:

fn decrypt(encrypted_buffer: Vec<u8>, borrowed_string: &String) { // How do we pass values to a function that we want to change?

to:

fn decrypt(encrypted_buffer: Vec<u8>, borrowed_string: &mut String) { 

Since we've updated the function to accept a &mut reference, we also need to modify the caller accordingly. In the main() function, change:

let party_foul = String::from("Using memory unsafe languages is a: "); // Is this variable changeable?
decrypt(encrypted_buffer, &party_foul); // Is this the correct way to pass a value to a function so that it can be changed?

to:

let mut party_foul = String::from("Using memory unsafe languages is a: "); 
decrypt(encrypted_buffer, &mut party_foul);

With these changes in place, recompile the project and this time, the program should run successfully and print the flag.

┌──(kali㉿kali)-[~/Desktop/pico-ctf/fixme2]
└─$ cargo run
Compiling rust_proj v0.1.0 (/home/kali/Desktop/pico-ctf/fixme2)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.34s
Running `target/debug/rust_proj`
Using memory unsafe languages is a: PARTY FOUL! Here is your flag: picoCTF{4r3_y0u_h4v1n5_fun_y31?}

🔐 Key takeaways

  • &T → Immutable reference: you cannot modify the data.
  • &mut T → Mutable reference: you can modify the data.
  • If a function needs to mutate a value, it must accept a &mut reference.
  • The original variable must be declared as mut to be passed as mutable.

🚩Flag

picoCTF{4r3_y0u_h4v1n5_fun_y31?}