CVE-2021-41242 (published 2021-12-10T23:15Z)

OpenOlat is a web-basedlearning management system. A path traversal vulnerability exists in OpenOlat prior to versions 15.5.12 and 16.0.5. By providing a filename that contains a relative path as a parameter in some REST methods, it is possible to create directory structures and write files anywhere on the target system. The attack could be used to write files anywhere in the web root folder or outside, depending on the configuration of the system and the properly configured permission of the application server user. The attack requires an OpenOlat user account, an enabled REST API and the rights on a business object to call the vulnerable REST calls. The problem is fixed in version 15.5.12 and 16.0.5. There is a workaround available. The vulnerability requires the REST module to be enabled. Disabling the REST module or limiting the REST module via some firewall or web-server access rules to be accessed only be trusted systems will mitigate the risk.

Severity: HIGH
CVSS v2 base score: 7.9 (AV:N/AC:M/Au:S/C:N/I:C/A:C)
CVSS v3 base score: 8.1, HIGH (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/OpenOLAT/OpenOLAT
commit_details: repo_url: https://github.com/OpenOLAT/OpenOLAT
author: srosse
author_date: 2021-11-02 10:04:49+01:00
msg: OO-5819: clean starting / for backward compatibility
repository: repo_url: https://github.com/OpenOLAT/OpenOLAT
repo_name: OpenOLAT/OpenOLAT
description: Learning Management System OpenOlat
repo_language: Java
owner: OpenOLAT
file_changes: filename: LocalFolderImpl.java
change_type: ModificationType.MODIFY
diff: @@ -373,6 +373,7 @@ public class LocalFolderImpl extends LocalImpl implements VFSContainer {
 
 	@Override
 	public VFSContainer createChildContainer(String name) {
+		name = cleanFilename(name); // backward compatibility
 		File fNewFile = new File(getBasefile(), name);
 		if(!isInPath(name)) {
 			log.warn("Could not create a new container::{} in container::{} - file out of parent directory", name, getBasefile().getAbsolutePath());
@@ -388,6 +389,7 @@ public class LocalFolderImpl extends LocalImpl implements VFSContainer {
 
 	@Override
 	public VFSLeaf createChildLeaf(String name) {
+		name = cleanFilename(name); // backward compatibility
 		File fNewFile = new File(getBasefile(), name);
 		try {
 			if(!isInPath(name)) {
@@ -407,6 +409,20 @@ public class LocalFolderImpl extends LocalImpl implements VFSContainer {
 		}
 		return new LocalFileImpl(fNewFile, this);
 	}
+	
+	/**
+	 * It was allowed to have a filename starting with /, but
+	 * it's not legal.
+	 * 
+	 * @param name The name
+	 * @return Name without starting /
+	 */
+	private String cleanFilename(String name) {
+		if(name != null && name.startsWith("/")) {
+			name = name.substring(1, name.length());
+		}
+		return name;
+	}
 
 	@Override
 	public VFSItemFilter getDefaultItemFilter() {

programming_language: Java
repo_url: https://github.com/OpenOLAT/OpenOLAT
commit_details: repo_url: https://github.com/OpenOLAT/OpenOLAT
author: srosse
author_date: 2021-11-01 11:48:12+01:00
msg: OO-5819: container can only create file in its own path
repository: repo_url: https://github.com/OpenOLAT/OpenOLAT
repo_name: OpenOLAT/OpenOLAT
description: Learning Management System OpenOlat
repo_language: Java
owner: OpenOLAT
file_changes: filename: LocalFolderImpl.java
change_type: ModificationType.MODIFY
diff: @@ -374,7 +374,13 @@ public class LocalFolderImpl extends LocalImpl implements VFSContainer {
 	@Override
 	public VFSContainer createChildContainer(String name) {
 		File fNewFile = new File(getBasefile(), name);
-		if (!fNewFile.mkdir()) return null;
+		if(!isInPath(name)) {
+			log.warn("Could not create a new container::{} in container::{} - file out of parent directory", name, getBasefile().getAbsolutePath());
+			return null;
+		}
+		if (!fNewFile.mkdir()) {
+			return null;
+		}
 		LocalFolderImpl locFI =  new LocalFolderImpl(fNewFile, this);
 		locFI.setDefaultItemFilter(defaultFilter);
 		return locFI;
@@ -384,15 +390,19 @@ public class LocalFolderImpl extends LocalImpl implements VFSContainer {
 	public VFSLeaf createChildLeaf(String name) {
 		File fNewFile = new File(getBasefile(), name);
 		try {
+			if(!isInPath(name)) {
+				log.warn("Could not create a new leaf::{} in container::{} - file out of parent directory", name, getBasefile().getAbsolutePath());
+				return null;
+			}
 			if(!fNewFile.getParentFile().exists()) {
 				fNewFile.getParentFile().mkdirs();
 			}
 			if (!fNewFile.createNewFile()) {
-				log.warn("Could not create a new leaf::" + name + " in container::" + getBasefile().getAbsolutePath() + " - file alreay exists");
+				log.warn("Could not create a new leaf::{} in container::{} - file alreay exists", name, getBasefile().getAbsolutePath());
 				return null;
 			} 
 		} catch (Exception e) {
-			log.error("Error while creating child leaf::" + name + " in container::" + getBasefile().getAbsolutePath(), e);
+			log.error("Error while creating child leaf::{} in container::{}", name, getBasefile().getAbsolutePath(), e);
 			return null;
 		}
 		return new LocalFileImpl(fNewFile, this);

programming_language: Java
filename: VFSContainer.java
change_type: ModificationType.MODIFY
diff: @@ -78,14 +78,14 @@ public interface VFSContainer extends VFSItem {
 	 * Create a new child container (of same type) if possible.
 	 * 
 	 * @param name
-	 * @return VFSItem if successfull, null otherwise.
+	 * @return VFSItem if successful, null otherwise.
 	 */
 	public VFSContainer createChildContainer(String name);
 	
 	/**
 	 * Create a new leaf (of same type) if possible,
 	 * @param name
-	 * @return VFSItem if successfull, null otherwise.
+	 * @return VFSItem if successful, null otherwise.
 	 */
 	public VFSLeaf createChildLeaf(String name);
 	

programming_language: Java
filename: ForumWebService.java
change_type: ModificationType.MODIFY
diff: @@ -598,7 +598,7 @@ public class ForumWebService {
 		return attachToPost(messageKey, filename, in, request);
 	}
 	
-	protected Response attachToPost(Long messageKey, String filename, InputStream file,  HttpServletRequest request) {
+	private Response attachToPost(Long messageKey, String filename, InputStream file,  HttpServletRequest request) {
 		//load message
 		Message mess = fom.loadMessage(messageKey);
 		if(mess == null) {
@@ -610,7 +610,7 @@ public class ForumWebService {
 		return attachToPost(mess, filename, file, request);
 	}
 
-	protected Response attachToPost(Message mess, String filename, InputStream file,  HttpServletRequest request) {
+	private Response attachToPost(Message mess, String filename, InputStream file,  HttpServletRequest request) {
 		Identity identity = getIdentity(request);
 		if(identity == null) {
 			return Response.serverError().status(Status.UNAUTHORIZED).build();

programming_language: Java
filename: CourseElementWebService.java
change_type: ModificationType.MODIFY
diff: @@ -1365,9 +1365,9 @@ public class CourseElementWebService extends AbstractCourseNodeWebService {
 			String filename = reader.getValue("filename", "task");
 			String taskFolderPath = TACourseNode.getTaskFolderPathRelToFolderRoot(course, parentNode.getCourseNode());
 			VFSContainer taskFolder = VFSManager.olatRootContainer(taskFolderPath, null);
-			VFSLeaf singleFile = (VFSLeaf) taskFolder.resolve("/" + filename);
+			VFSLeaf singleFile = (VFSLeaf)taskFolder.resolve(filename);
 			if (singleFile == null) {
-				singleFile = taskFolder.createChildLeaf("/" + filename);
+				singleFile = taskFolder.createChildLeaf(filename);
 			}
 			File file = reader.getFile();
 			if(file != null) {
@@ -2314,9 +2314,9 @@ public class CourseElementWebService extends AbstractCourseNodeWebService {
 			if(STCourseNodeEditController.CONFIG_VALUE_DISPLAY_FILE.equals(moduleConfig.getStringValue(STCourseNodeEditController.CONFIG_KEY_DISPLAY_TYPE))) {
 				if(in != null && StringHelper.containsNonWhitespace(filename)) {
 					VFSContainer rootContainer = course.getCourseFolderContainer();
-					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
+					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
 					if (singleFile == null) {
-						singleFile = rootContainer.createChildLeaf("/" + filename);
+						singleFile = rootContainer.createChildLeaf(filename);
 					}
 	
 					moduleConfig.set(STCourseNodeEditController.CONFIG_KEY_FILE, "/" + filename);
@@ -2326,7 +2326,7 @@ public class CourseElementWebService extends AbstractCourseNodeWebService {
 					FileUtils.closeSafely(in);
 				} else if (StringHelper.containsNonWhitespace(filename)) {
 					VFSContainer rootContainer = course.getCourseFolderContainer();
-					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
+					VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
 					if(singleFile != null) {
 						moduleConfig.set(STCourseNodeEditController.CONFIG_KEY_FILE, "/" + filename);
 					}
@@ -2487,9 +2487,9 @@ public class CourseElementWebService extends AbstractCourseNodeWebService {
 		public void configure(ICourse course, CourseNode newNode, ModuleConfiguration moduleConfig) {
 			newNode.setDisplayOption(CourseNode.DISPLAY_OPTS_TITLE_DESCRIPTION_CONTENT);
 			VFSContainer rootContainer = course.getCourseFolderContainer();
-			VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve("/" + filename);
+			VFSLeaf singleFile = (VFSLeaf) rootContainer.resolve(filename);
 			if (singleFile == null) {
-				singleFile = rootContainer.createChildLeaf("/" + filename);
+				singleFile = rootContainer.createChildLeaf(filename);
 			}
 
 			if(in != null) {

programming_language: Java
filename: MultipartReader.java
change_type: ModificationType.MODIFY
diff: @@ -58,7 +58,7 @@ public class MultipartReader {
 				if(part.getContentType() != null && (StringHelper.containsNonWhitespace(part.getSubmittedFileName()) || !part.getContentType().startsWith("text/plain"))) {
 					contentType = part.getContentType();
 					filename = part.getSubmittedFileName();
-					if(filename != null) {
+					if(filename != null && !filename.contains("..")) {
 						filename = UUID.randomUUID().toString().replace("-", "") + "_" + filename;
 					} else {
 						filename = "upload-" + UUID.randomUUID().toString().replace("-", "");

programming_language: Java
filename: ForumTest.java
change_type: ModificationType.MODIFY
diff: @@ -387,6 +387,41 @@ public class ForumTest extends OlatRestTestCase {
 		conn.shutdown();
 	}
 	
+	@Test
+	public void testUploadAttachmentOutOfBox() throws IOException, URISyntaxException {
+		RestConnection conn = new RestConnection();
+		assertTrue(conn.login(id1));
+		
+		URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString())
+			.queryParam("authorKey", id1.getKey())
+			.queryParam("title", "New message with attachment ")
+			.queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
+		HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
+		HttpResponse response = conn.execute(method);
+		assertEquals(200, response.getStatusLine().getStatusCode());
+		MessageVO message = conn.parse(response, MessageVO.class);
+		assertNotNull(message);
+		
+		//attachment
+		URL portraitUrl = CoursesElementsTest.class.getResource("portrait.jpg");
+		assertNotNull(portraitUrl);
+		File portrait = new File(portraitUrl.toURI());
+		
+		//upload portrait
+		URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
+		HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
+		conn.addMultipart(attachMethod, "../../portrait.jpg", portrait);
+		HttpResponse attachResponse = conn.execute(attachMethod);
+		assertEquals(200, attachResponse.getStatusLine().getStatusCode());
+		
+		//check if the file exists
+		VFSContainer container = forumManager.getMessageContainer(message.getForumKey(), message.getKey());
+		VFSItem uploadedFile = container.resolve("portrait.jpg");
+		Assert.assertNull(uploadedFile);
+		
+		conn.shutdown();
+	}
+	
 	@Test
 	public void testUpload64Attachment() throws IOException, URISyntaxException {
 		RestConnection conn = new RestConnection();

programming_language: Java