VAITP Dataset

← Back to the dataset

CVE-2012-0215

Improper access control in Trytond before 2.4.0 allows remote authenticated users to modify user privileges via certain RPC calls

  • CVSS 5.5
  • CWE-264
  • Authentication, Authorization, and Session Management
  • Remote

model/modelstorage.py in the Tryton application framework (trytond) before 2.4.0 for Python does not properly restrict access to the Many2Many field in the relation model, which allows remote authenticated users to modify the privileges of arbitrary users via a (1) create, (2) write, (3) delete, or (4) copy rpc call.

CVSS base score
5.5
Published
2012-07-12
OWASP
A01 Broken Access Control
Orthogonal defect classification
Checking
Code defect classification
Incorrect Check
Category
Authentication, Authorization, and Session Management
Subcategory
Privilege Escalation
Accessibility scope
Remote
Impact
Privilege Escalation
Fixed by upgrading
Yes

Solution

Update trytond to version 2.4.0 or higher.

Vulnerable code sample

class ModelStorage:
    def __init__(self, model_name):
        self.model_name = model_name
        self.data = {}

    def create(self, values, user_id):
        new_id = len(self.data) + 1
        self.data[new_id] = values
        return new_id

    def read(self, ids, fields, user_id):
        results = []
        for record_id in ids:
            if record_id in self.data:
                results.append({field: self.data[record_id].get(field) for field in fields})
            else:
                results.append(None)
        return results

    def write(self, ids, values, user_id):
      for record_id in ids:
        if record_id in self.data:
            self.data[record_id].update(values)
      return True

    def delete(self, ids, user_id):
      for record_id in ids:
        if record_id in self.data:
          del self.data[record_id]
      return True

    def copy(self, ids, default_values, user_id):
        new_ids = []
        for record_id in ids:
            if record_id in self.data:
                new_id = len(self.data) + 1
                new_record = self.data[record_id].copy()
                new_record.update(default_values)
                self.data[new_id] = new_record
                new_ids.append(new_id)
        return new_ids

class User:
    def __init__(self, user_id, name, groups=None):
        self.id = user_id
        self.name = name
        self.groups = groups if groups else []

class Group:
    def __init__(self, group_id, name):
        self.id = group_id
        self.name = name

class Model:
    def __init__(self, model_storage):
        self.storage = model_storage

    def create(self, values, user_id):
        return self.storage.create(values, user_id)

    def read(self, ids, fields, user_id):
        return self.storage.read(ids, fields, user_id)
    
    def write(self, ids, values, user_id):
       return self.storage.write(ids, values, user_id)

    def delete(self, ids, user_id):
       return self.storage.delete(ids, user_id)
    
    def copy(self, ids, default_values, user_id):
        return self.storage.copy(ids, default_values, user_id)

Patched code sample

from enum import Enum
from typing import Dict, List, Optional, Set, Union
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class Operation(Enum):
    CREATE = 'create'
    READ = 'read'
    WRITE = 'write'
    DELETE = 'delete'
    COPY = 'copy'

class AccessError(Exception):
    pass

class AccessControl:
    
    PROTECTED_MODELS = {
        'res.user',
        'res.group',
        'res.role',
        'res.permission'
    }
    
    PROTECTED_FIELDS = {
        'res.user': {'groups', 'roles', 'permissions'},
        'res.group': {'users', 'permissions'},
        'res.role': {'users', 'permissions'}
    }
    
    REQUIRED_GROUPS = {
        'res.user': {
            Operation.CREATE: {'admin', 'user_manager'},
            Operation.WRITE: {'admin', 'user_manager'},
            Operation.DELETE: {'admin'},
            Operation.COPY: {'admin', 'user_manager'}
        }
    }
    
    def __init__(self):
        
        self.audit_log = []
        
    def _validate_user(self, user: Dict[str, Union[int, List[str]]]) -> None:
        
        if not isinstance(user, dict):
            raise AccessError("Invalid user data")
            
        if 'id' not in user or not isinstance(user['id'], int):
            raise AccessError("Invalid user ID")
            
        if 'groups' not in user or not isinstance(user['groups'], list):
            raise AccessError("Invalid user groups")
            
    def _validate_operation(
        self,
        operation: str,
        model_name: str,
        field_name: Optional[str] = None
    ) -> None:
        
        try:
            Operation(operation)
        except ValueError:
            raise AccessError(f"Invalid operation: {operation}")
            
        if not isinstance(model_name, str):
            raise AccessError("Invalid model name")
            
        if field_name and not isinstance(field_name, str):
            raise AccessError("Invalid field name")
            
    def _log_access(
        self,
        user_id: int,
        operation: str,
        model_name: str,
        success: bool,
        details: str
    ) -> None:
        entry = {
            'user_id': user_id,
            'operation': operation,
            'model_name': model_name,
            'success': success,
            'details': details
        }
        self.audit_log.append(entry)
        
        if not success:
            logger.warning(
                f"Access denied: user={user_id} op={operation} "
                f"model={model_name} details={details}"
            )
            
    def check_access(
        self,
        model_name: str,
        field_name: Optional[str],
        operation: str,
        user: Dict[str, Union[int, List[str]]]
    ) -> bool:
        
        try:
            self._validate_user(user)
            self._validate_operation(operation, model_name, field_name)
            
            user_id = user['id']
            user_groups = set(user['groups'])
            
            if model_name in self.PROTECTED_MODELS:
                if field_name and field_name in self.PROTECTED_FIELDS.get(model_name, set()):
                    required_groups = self.REQUIRED_GROUPS.get(model_name, {}).get(
                        Operation(operation),
                        set()
                    )
                    
                    if not required_groups & user_groups:
                        self._log_access(
                            user_id,
                            operation,
                            model_name,
                            False,
                            "Insufficient permissions"
                        )
                        return False
                        
            self._log_access(
                user_id,
                operation,
                model_name,
                True,
                "Access granted"
            )
            return True
            
        except Exception as e:
            self._log_access(
                user.get('id', 0),
                operation,
                model_name,
                False,
                str(e)
            )
            raise AccessError(f"Access check failed: {str(e)}")

class RecordManager:
    
    def __init__(self):

        self.access_control = AccessControl()
        
    def _check_access(
        self,
        model_name: str,
        operation: str,
        user: Dict[str, Union[int, List[str]]]
    ) -> None:

        if not self.access_control.check_access(model_name, "groups", operation, user):
            raise AccessError("Access denied")
            
    def create_record(
        self,
        model_name: str,
        data: Dict[str, Any],
        user: Dict[str, Union[int, List[str]]]
    ) -> Dict[str, Any]:

        self._check_access(model_name, Operation.CREATE.value, user)
        return {model_name: data}
        
    def write_record(
        self,
        model_name: str,
        record_id: int,
        data: Dict[str, Any],
        user: Dict[str, Union[int, List[str]]]
    ) -> Dict[str, Any]:

        self._check_access(model_name, Operation.WRITE.value, user)
        return {model_name: {record_id: data}}
        
    def delete_record(
        self,
        model_name: str,
        record_id: int,
        user: Dict[str, Union[int, List[str]]]
    ) -> Dict[str, Any]:

        self._check_access(model_name, Operation.DELETE.value, user)
        return {model_name: record_id}
        
    def copy_record(
        self,
        model_name: str,
        record_id: int,
        user: Dict[str, Union[int, List[str]]]
    ) -> Dict[str, Any]:

        self._check_access(model_name, Operation.COPY.value, user)
        return {model_name: record_id}

Cite this entry

@misc{vaitp:cve20120215,
  title        = {{Improper access control in Trytond before 2.4.0 allows remote authenticated users to modify user privileges via certain RPC calls}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2012},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2012-0215},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2012-0215/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::